C# 操作SQL Server数据库, 执行T-SQL语句, 将记录结果填充到DataSet中

C# 操作SQL Server数据库, 执行T-SQL语句, 将记录结果填充到DataSet中 
using System;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApp5
{
    class Program
    {
        // INSERT, UPDATE, DELETE
        static void Main(string[] args)
        {
            /*
                --创建数据表
                CREATE TABLE [Users](
                        [Id] [int] IDENTITY(1,1),--主键
                        [Name] [nvarchar](50) NOT NULL,--名称
                        [Sex] [nvarchar](10),--性别
                        PRIMARY KEY([Name])
                );
                INSERT INTO [Users]([Name],[Sex]) VALUES ('Alice','Female');
                INSERT INTO [Users]([Name],[Sex]) VALUES ('Amy','Female');
                INSERT INTO [Users]([Name],[Sex]) VALUES ('Bruce','Male');
             */

            string connectionString = "Data Source=192.168.0.110,1433;Initial Catalog=TestDB;uid=sa;pwd=123456";
            string sqlstring = "SELECT * FROM [Users];";

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    conn.Open();
                    cmd.Connection = conn;
                    cmd.CommandText = sqlstring;
                    cmd.CommandType = System.Data.CommandType.Text;

                    using (DataSet ds = new DataSet())
                    {
                        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                        {
                            da.Fill(ds);
                        }
                        // return ds;
                    }
                }
            }
  
            Console.Read();
        }
    }
}

作者最新文章
ASP.NET aspx获取服务器信息, 操作系统, .NET版本, IIS版本
C# Unix转换本地时间, Unix时间戳转换DateTime
C# 获取Unix时间戳
C# DateTime.Ticks 转换为 DateTime
C# 数字转换字符串左边补0, 数字前补0的方法