当前位置:编程学习 > C#/ASP.NET >>

请大师帮忙,从MS SQL Server把table作export,产生txt File

MS SQL server的Northwind
如何让SELECT * FROM Customers
写入txt文件
String靠左对齐,不足的部分补空格键
String长度请参考下图红框
谢谢
--------------------编程问答-------------------- 可以试试 :
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

insert into 
OpenRowset('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=d:\;',
'select *from [22.txt] ')
select * from Products

---关闭
exec sp_configure 'show advanced options',0
reconfigure --------------------编程问答--------------------
引用 1 楼 earlsen 的回复:
可以试试 :
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

insert into 
OpenRowset('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=d:\;',
'select *from [22.txt] ')
select * from Products

---关闭
exec sp_configure 'show advanced options',0
reconfigure

这是T-SQL吗?
这对我太难了
有没有C#的代码
谢谢
--------------------编程问答-------------------- 先读到datatable里,再导出txt,2步 --------------------编程问答--------------------
引用 3 楼 good_jobs 的回复:
先读到datatable里,再导出txt,2步

你是指先temp table
然后再变成txt吗?
可是这样我还是不知代码要怎么写
麻烦教一下
谢谢
--------------------编程问答-------------------- 再次求救
有没有人可以帮忙,谢谢!
--------------------编程问答-------------------- 是不是类似写txt的log档?? --------------------编程问答--------------------
引用 6 楼 slcboy 的回复:
是不是类似写txt的log档??

差不多是这样
好写吗?
谢谢
--------------------编程问答-------------------- 先读出来数据,在通过文件流将数据写入文本信息。 --------------------编程问答--------------------
引用 8 楼 a876880345 的回复:
先读出来数据,在通过文件流将数据写入文本信息。

要如何读数据呢?
代码要如何写呢?
谢谢
--------------------编程问答-------------------- 我觉得 你应该找本书来看,这些基本操作书上都有的。直接查看相应章节就可以了。 --------------------编程问答--------------------
引用 9 楼 u011357717 的回复:
Quote: 引用 8 楼 a876880345 的回复:

先读出来数据,在通过文件流将数据写入文本信息。

要如何读数据呢?
代码要如何写呢?
谢谢
不知道你用的是什么编程语言,
.Net使用ADO.NET... --------------------编程问答--------------------
引用 11 楼 a876880345 的回复:
Quote: 引用 9 楼 u011357717 的回复:

Quote: 引用 8 楼 a876880345 的回复:

先读出来数据,在通过文件流将数据写入文本信息。

要如何读数据呢?
代码要如何写呢?
谢谢
不知道你用的是什么编程语言,
.Net使用ADO.NET...

只要C#可以写出来即可
我有买书
可是书上都没教怎么把data转出来
麻烦教教我
谢谢
--------------------编程问答-------------------- 先从数据库中通过sql查询将数据写到datatable中;再将datatable中的数据逐条写入文件。

    class SqlManager
    {
        /// <summary>
        /// 连接sqlserver对象
        /// </summary>
        private SqlConnection m_conn;
        /// <summary>
        /// 数据库名称,需要根据使用的数据库修改
        /// </summary>
        public  string m_DatabaseName = "";
        /// <summary>
        /// 服务器IP或名称
        /// </summary>
        private string m_ServerName = "";
        /// <summary>
        /// 用户名
        /// </summary>
        private string m_UserName = "";
        /// <summary>
        /// 密码
        /// </summary>
        private string m_Password = "";

        public DbInterface(string serverName, string userName, string passWord, string dataBase)
        {
            m_ServerName = serverName;
            m_UserName = userName;
            m_Password = passWord;
            m_DatabaseName = dataBase;
        }

        /// <summary>
        /// 数据库连接
        /// </summary>
        /// <returns>是否连接</returns>
        private bool Connection()
        {
            bool flag = false;
            try
            {
                String connStr = "User ID=" + m_UserName + ";Password=" + m_Password + ";database="
                + m_DatabaseName + ";Data Source =" + m_ServerName;
                m_conn = new SqlConnection(connStr);
                
                if (m_conn.State != ConnectionState.Open)
                {
                    m_conn.Open();
                }
                flag = true;
            }
            catch (Exception ex)
            {
                flag = false;
                //throw ex;
            }
            return flag;
        }
        /// <summary>
        /// 读取数据       
         /// </summary>
        /// <returns></returns>
        public DataTable GetNetPower(string queryStr)
        {
            DataTable dt = null;
            if (Connection())
            {
                try
                {
                    dt = new DataTable();
                    SqlCommand cmd = new SqlCommand(queryStr, m_conn);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    adapter.Fill(dt);//填充数据表
                      cmd.Dispose();
                }
                catch (Exception ex)
                {
                    dt = null;
                }
                finally
                {
                    m_conn.Close();
                    m_conn.Dispose();
                }
            }

            return dt;
        }
    }
}


--------------------编程问答--------------------
引用 13 楼 xian_wwq 的回复:
先从数据库中通过sql查询将数据写到datatable中;再将datatable中的数据逐条写入文件。

    class SqlManager
    {
        /// <summary>
        /// 连接sqlserver对象
        /// </summary>
        private SqlConnection m_conn;
        /// <summary>
        /// 数据库名称,需要根据使用的数据库修改
        /// </summary>
        public  string m_DatabaseName = "";
        /// <summary>
        /// 服务器IP或名称
        /// </summary>
        private string m_ServerName = "";
        /// <summary>
        /// 用户名
        /// </summary>
        private string m_UserName = "";
        /// <summary>
        /// 密码
        /// </summary>
        private string m_Password = "";

        public DbInterface(string serverName, string userName, string passWord, string dataBase)
        {
            m_ServerName = serverName;
            m_UserName = userName;
            m_Password = passWord;
            m_DatabaseName = dataBase;
        }

        /// <summary>
        /// 数据库连接
        /// </summary>
        /// <returns>是否连接</returns>
        private bool Connection()
        {
            bool flag = false;
            try
            {
                String connStr = "User ID=" + m_UserName + ";Password=" + m_Password + ";database="
                + m_DatabaseName + ";Data Source =" + m_ServerName;
                m_conn = new SqlConnection(connStr);
                
                if (m_conn.State != ConnectionState.Open)
                {
                    m_conn.Open();
                }
                flag = true;
            }
            catch (Exception ex)
            {
                flag = false;
                //throw ex;
            }
            return flag;
        }
        /// <summary>
        /// 读取数据       
         /// </summary>
        /// <returns></returns>
        public DataTable GetNetPower(string queryStr)
        {
            DataTable dt = null;
            if (Connection())
            {
                try
                {
                    dt = new DataTable();
                    SqlCommand cmd = new SqlCommand(queryStr, m_conn);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    adapter.Fill(dt);//填充数据表
                      cmd.Dispose();
                }
                catch (Exception ex)
                {
                    dt = null;
                }
                finally
                {
                    m_conn.Close();
                    m_conn.Dispose();
                }
            }

            return dt;
        }
    }
}


不好意思这么迟才看到
明天我会好好的研究
谢谢
--------------------编程问答--------------------
引用 13 楼 xian_wwq 的回复:
先从数据库中通过sql查询将数据写到datatable中;再将datatable中的数据逐条写入文件。

    class SqlManager
    {
        /// <summary>
        /// 连接sqlserver对象
        /// </summary>
        private SqlConnection m_conn;
        /// <summary>
        /// 数据库名称,需要根据使用的数据库修改
        /// </summary>
        public  string m_DatabaseName = "";
        /// <summary>
        /// 服务器IP或名称
        /// </summary>
        private string m_ServerName = "";
        /// <summary>
        /// 用户名
        /// </summary>
        private string m_UserName = "";
        /// <summary>
        /// 密码
        /// </summary>
        private string m_Password = "";

        public DbInterface(string serverName, string userName, string passWord, string dataBase)
        {
            m_ServerName = serverName;
            m_UserName = userName;
            m_Password = passWord;
            m_DatabaseName = dataBase;
        }

        /// <summary>
        /// 数据库连接
        /// </summary>
        /// <returns>是否连接</returns>
        private bool Connection()
        {
            bool flag = false;
            try
            {
                String connStr = "User ID=" + m_UserName + ";Password=" + m_Password + ";database="
                + m_DatabaseName + ";Data Source =" + m_ServerName;
                m_conn = new SqlConnection(connStr);
                
                if (m_conn.State != ConnectionState.Open)
                {
                    m_conn.Open();
                }
                flag = true;
            }
            catch (Exception ex)
            {
                flag = false;
                //throw ex;
            }
            return flag;
        }
        /// <summary>
        /// 读取数据       
         /// </summary>
        /// <returns></returns>
        public DataTable GetNetPower(string queryStr)
        {
            DataTable dt = null;
            if (Connection())
            {
                try
                {
                    dt = new DataTable();
                    SqlCommand cmd = new SqlCommand(queryStr, m_conn);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    adapter.Fill(dt);//填充数据表
                      cmd.Dispose();
                }
                catch (Exception ex)
                {
                    dt = null;
                }
                finally
                {
                    m_conn.Close();
                    m_conn.Dispose();
                }
            }

            return dt;
        }
    }
}



真不好意思
我是初学者
代码
我要放在VS上
我都弄不好
方便帮我把代码放在VS上吗?
谢谢
--------------------编程问答-------------------- 不能什么都找人代劳,百度下vs如何创建工程吧 --------------------编程问答-------------------- namespace BinaryWriterReader
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Binary Writers / Readers *****\n");

            // Open a binary writer for a file.
            FileInfo f = new FileInfo("BinFile.dat");
            using (BinaryWriter bw = new BinaryWriter(f.OpenWrite()))
            {
                // Print out the type of BaseStream.
                // (System.IO.FileStream in this case).
                Console.WriteLine("Base stream is: {0}", bw.BaseStream);

                // Create some data to save in the file
                double aDouble = 1234.67;
                int anInt = 34567;
                string aString = "A, B, C";

                // Write the data
                bw.Write(aDouble);
                bw.Write(anInt);
                bw.Write(aString);
            }

            // Read the binary data from the stream.
            using (BinaryReader br = new BinaryReader(f.OpenRead()))
            {
                Console.WriteLine(br.ReadDouble());
                Console.WriteLine(br.ReadInt32());
                Console.WriteLine(br.ReadString());
            }

            Console.ReadLine();
        }

    }
} --------------------编程问答-------------------- using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace BinaryWriterReader
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Binary Writers / Readers *****\n");

            // Open a binary writer for a file.
            FileInfo f = new FileInfo("BinFile.dat");
            using (BinaryWriter bw = new BinaryWriter(f.OpenWrite()))
            {
                // Print out the type of BaseStream.
                // (System.IO.FileStream in this case).
                Console.WriteLine("Base stream is: {0}", bw.BaseStream);

                // Create some data to save in the file
                double aDouble = 1234.67;
                int anInt = 34567;
                string aString = "A, B, C";

                // Write the data
                bw.Write(aDouble);
                bw.Write(anInt);
                bw.Write(aString);
            }

            // Read the binary data from the stream.
            using (BinaryReader br = new BinaryReader(f.OpenRead()))
            {
                Console.WriteLine(br.ReadDouble());
                Console.WriteLine(br.ReadInt32());
                Console.WriteLine(br.ReadString());
            }

            Console.ReadLine();
        }

    }
}
--------------------编程问答--------------------
引用 18 楼 fgrs_work 的回复:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace BinaryWriterReader
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Binary Writers / Readers *****\n");

            // Open a binary writer for a file.
            FileInfo f = new FileInfo("BinFile.dat");
            using (BinaryWriter bw = new BinaryWriter(f.OpenWrite()))
            {
                // Print out the type of BaseStream.
                // (System.IO.FileStream in this case).
                Console.WriteLine("Base stream is: {0}", bw.BaseStream);

                // Create some data to save in the file
                double aDouble = 1234.67;
                int anInt = 34567;
                string aString = "A, B, C";

                // Write the data
                bw.Write(aDouble);
                bw.Write(anInt);
                bw.Write(aString);
            }

            // Read the binary data from the stream.
            using (BinaryReader br = new BinaryReader(f.OpenRead()))
            {
                Console.WriteLine(br.ReadDouble());
                Console.WriteLine(br.ReadInt32());
                Console.WriteLine(br.ReadString());
            }

            Console.ReadLine();
        }

    }
}

感谢您的帮忙
由于计算机放在公司
没办法测试
我星期一上班时会测试看看
谢谢
--------------------编程问答--------------------
引用 18 楼 fgrs_work 的回复:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace BinaryWriterReader
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Binary Writers / Readers *****\n");

            // Open a binary writer for a file.
            FileInfo f = new FileInfo("BinFile.dat");
            using (BinaryWriter bw = new BinaryWriter(f.OpenWrite()))
            {
                // Print out the type of BaseStream.
                // (System.IO.FileStream in this case).
                Console.WriteLine("Base stream is: {0}", bw.BaseStream);

                // Create some data to save in the file
                double aDouble = 1234.67;
                int anInt = 34567;
                string aString = "A, B, C";

                // Write the data
                bw.Write(aDouble);
                bw.Write(anInt);
                bw.Write(aString);
            }

            // Read the binary data from the stream.
            using (BinaryReader br = new BinaryReader(f.OpenRead()))
            {
                Console.WriteLine(br.ReadDouble());
                Console.WriteLine(br.ReadInt32());
                Console.WriteLine(br.ReadString());
            }

            Console.ReadLine();
        }

    }
}

请问这个程序代码在作什么用呢?
看不太懂,
能否解说一下
谢谢!
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,