标题:C#写代码需要从SQL中取出数据并存入TXT中并且存入的路径是网络参数,现在新 ...
只看楼主
tatsuo
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2016-11-1
结帖率:0
 问题点数:0 回复次数:4 
C#写代码需要从SQL中取出数据并存入TXT中并且存入的路径是网络参数,现在新建TXT都出错,望大神能帮忙解决

C#中报错错误
1    应输入 class、delegate、enum、interface 或 struct    C:\Users\ndcsh_admin\Desktop\SAP\文档\SAP01\SAP01\MainWindow.xaml.cs    12    40    SAP01

2      应输入标识符    C:\Users\ndcsh_admin\Desktop\SAP\文档\SAP01\SAP01\MainWindow.xaml.cs    15    39    SAP01

3    应输入类型、命名空间定义或文件尾    C:\Users\ndcsh_admin\Desktop\SAP\文档\SAP01\SAP01\MainWindow.xaml.cs    16    14    SAP01
   
4    未能找到类型或命名空间名称“?Attribute”(是否缺少 using 指令或程序集引用?)    C:\Users\ndcsh_admin\Desktop\SAP\文档\SAP01\SAP01\MainWindow.xaml.cs    15    38    SAP01
代码如下
public static void ReadOrderData(string connectionString)
{
//要执行的Sql语句
  string queryString = new SqlDataAdapter("sql查询语句", "server=服务器地址;database=数据库,user=用户名;pwd=密码");
  using (SqlConnection connection = new SqlConnection( connectionString))
  {
    SqlCommand command = new SqlCommand( queryString, connection);
    connection.Open();
    using (SqlDataReader sdr = cmd.ExecuteReader())
        {
         using (StreanmWriter sw = new StreamWriter("D:\\test.txt"))
            {
                while  (sdr.Read())
                sw.Write("\\qu" + sdr[0].ToString()):
             }   
        }
    }
}
搜索更多相关主题的帖子: interface Desktop 空间名称 Users 网络 
2016-11-01 12:47
tatsuo
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2016-11-1
得分:0 
居然需要我自问自答,这。。。。。。。。。。。。。。。。。。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace SAP01
{
    class Program
    {
        static void Main(string[] args)
        {
            //新建一个数据库连接  
            //得到一个数据库连接字符串  
            string GetConnectString = ""sql查询语句", "server=服务器地址;database=数据库,user=用户名;pwd=密码"";
            using (SqlConnection conn = new SqlConnection(GetConnectString))
            {
                conn.Open();//打开数据库  
                //Console.WriteLine("数据库打开成功!");  
                //创建数据库命令  
                SqlCommand cmd = conn.CreateCommand();
                //创建查询语句  
                = "select * from new_ecc_deliveryconfirm";
                //从数据库中读取数据流存入reader中  
                SqlDataReader reader = cmd.ExecuteReader();

                //从reader中读取下一行数据,如果没有数据,reader.Read()返回flase
                string new_name;
                //格式输出数据  
                sw = new ("D:\\Text.Txt");
                sw.AutoFlush = true;
                Console.SetOut(sw);
                while (reader.Read())
                {
                    //reader.GetOrdinal("id")是得到ID所在列的index,  
                    //reader.GetInt32(int n)这是将第n列的数据以Int32的格式返回  
                    //reader.GetString(int n)这是将第n列的数据以string 格式返回  
                    new_name = reader.GetString(reader.GetOrdinal("new_name"));
                    Console.Write("new_name:{0};\n", new_name);
                }
                sw.Close();
            }
        }
    }
}
2016-11-01 16:11
tatsuo
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2016-11-1
得分:0 
完成一半有大神能帮我解决指定路径吗
2016-11-01 16:14
tatsuo
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2016-11-1
得分:0 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace SAP01
{
    class Program
    {
        static void Main(string[] args)
        {
            //新建一个数据库连接  
            //得到一个数据库连接字符串  
            string GetConnectString = "sql查询语句", "server=服务器地址;database=数据库,user=用户名;pwd=密码";
            using (SqlConnection conn = new SqlConnection(GetConnectString))
            {
                conn.Open();//打开数据库  
                //Console.WriteLine("数据库打开成功!");  
                //创建数据库命令  
                SqlCommand cmd = conn.CreateCommand();
                //创建查询语句  
                = "select * from new_ecc_deliveryconfirm";
                //从数据库中读取数据流存入reader中  
                SqlDataReader reader = cmd.ExecuteReader();

                //从reader中读取下一行数据,如果没有数据,reader.Read()返回flase
                string new_name;
                //格式输出数据  
                sw = new ("D:\\"+  +".Txt");
                sw.AutoFlush = true;
                Console.SetOut(sw);



                while (reader.Read())
                {

                    for (int i; i < reader.count; 1++)
                    {
                        
                         sw = new ("D:\\" + +".Txt");
                        sw.AutoFlush = true;
                        Console.SetOut(sw);
                        int flag = 0;
                        new_name = reader.GetString(reader.GetOrdinal("new_name"));

                        reader[i]
                        for (int i = 0; i < reader[i]["productid"]; i++)
                        {
                            Console.Write("new_name:{0};\n", reader[i]["productid"],flag);
                            flag += 10;
                        }
                        sw.Close();

                    }
                    //reader.GetOrdinal("id")是得到ID所在列的index,  
                    //reader.GetInt32(int n)这是将第n列的数据以Int32的格式返回  
                    //reader.GetString(int n)这是将第n列的数据以string 格式返回  
                    
                }
            }
        }
    }
}请大神能帮我继续完善吗
2016-11-01 21:17
tatsuo
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2016-11-1
得分:0 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Threading;
using System.Data;

namespace SAP01
{
    class Program
    {
        static void Main(string[] args)
        {
            //新建一个数据库连接  
            //得到一个数据库连接字符串  
            string GetConnectString = "sql查询语句", "server=服务器地址;database=数据库,user=用户名;pwd=密码";
            using (SqlConnection conn = new SqlConnection(GetConnectString))
            {
                conn.Open();//打开数据库  
                //Console.WriteLine("数据库打开成功!");  
                //创建数据库命令  
                SqlCommand cmd1 = conn.CreateCommand();
                //创建查询语句  
                = "sql语句 ;";

                //从数据库中读取数据流存入reader中  
                SqlDataReader reader = cmd1.ExecuteReader();
                定义 需要字段1
                while (reader.Read())
                {
                    需要字段1 = reader.GetString(reader.GetOrdinal("需要字段1"));
                }
                reader.Close();

                = "sql语句2";
        定义 需要字段2
                //格式输出数据  
                SqlDataReader reader2 = cmd1.ExecuteReader();
                while (reader2.Read())
                {
                    //reader.GetOrdinal("id")是得到ID所在列的index,  
                    //reader.GetInt32(int n)这是将第n列的数据以Int32的格式返回  
                    //reader.GetString(int n)这是将第n列的数据以string 格式返回  
                    需要字段2 = reader2.GetString(reader2.GetOrdinal("需要字段2"));
                     sw = new (需要字段1+"\\" 需要字段2 ".txt");
                    sw.AutoFlush = true;
                    Console.SetOut(sw);               
                    Console.Write("{0}\t{1}\t", 需要字段1,需要字段2);
                    }
                    sw.Close();
                }
            }
            while (true)
            {
                Thread.Sleep(3000000);//每五分钟循环输出
            }
        }
    }
}
最新理解写的模板与大家分享欢迎指出错误
2016-11-03 14:27



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-470557-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.526236 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved