ADO.NET 操作 SqlServer

ado.net 操作sql server

 

连接字符串:

public static string ConnectionString = "server=MRF10849\\MRF10849;uid=sa;pwd=mrf@2017;database=test";

cha

static void Insert()
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                connection.Open();
                using (SqlCommand cmd = connection.CreateCommand())
                {
                    cmd.CommandText = @"INSERT  INTO [Test].[dbo].[TableA]
                                                ( [Name], [CreateTime], [ParantId] )
                                        VALUES  ( 'test', '2017-01-01', 1 )";
                    if (cmd.ExecuteNonQuery() > 0)
                    {
                        Console.WriteLine("插入成功");
                    }
                    else
                    {
                        Console.WriteLine("插入失败");
                    }
                }
            }
        }

 

带参数的插入:

static void InsertByParameters()
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                connection.Open();
                using (SqlCommand cmd = connection.CreateCommand())
                {
                    cmd.CommandText = @"INSERT  INTO [Test].[dbo].[TableA]
                                                ( [Name], [CreateTime], [ParantId] )
                                        VALUES  ( @Name, @CreateTime, @ParantId )";
                    cmd.Parameters.Add("@Name", SqlDbType.NVarChar);
                    cmd.Parameters["@Name"].Value = "Sichuan";
                    cmd.Parameters.AddWithValue("@CreateTime", DateTime.Now);
                    cmd.Parameters.AddWithValue("@ParantId", 1);
                    if (cmd.ExecuteNonQuery() > 0)
                    {
                        Console.WriteLine("插入成功");
                    }
                    else
                    {
                        Console.WriteLine("插入失败");
                    }
                }
            }
        }

  

修改:

static void Update()
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                connection.Open();
                using (SqlCommand cmd = connection.CreateCommand())
                {
                    cmd.CommandText = @"UPDATE [Test].[dbo].[TableA] SET Name = '1111' WHERE [Id] = 1";
                    if (cmd.ExecuteNonQuery() > 0)
                    {
                        Console.WriteLine("修改成功");
                    }
                    else
                    {
                        Console.WriteLine("修改失败");
                    }
                }
            }
        }

  

删除:

static void Delete()
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                connection.Open();
                using (SqlCommand cmd = connection.CreateCommand())
                {
                    cmd.CommandText = @"DELETE FROM [Test].[dbo].[TableA] WHERE [Id] = 2";
                    if (cmd.ExecuteNonQuery() > 0)
                    {
                        Console.WriteLine("删除成功");
                    }
                    else
                    {
                        Console.WriteLine("删除失败");
                    }
                }
            }
        }

  

三种查询:

dataset

static void QueryDataByDataSet()
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                connection.Open();
                string sql = @"SELECT  [Id] ,
                                        [Name] ,
                                        [CreateTime] ,
                                        [ParantId]
                                FROM    [Test].[dbo].[TableA] WITH ( NOLOCK )";
                SqlDataAdapter sqlda = new SqlDataAdapter(sql, ConnectionString);
                DataSet ds = new DataSet();
                sqlda.Fill(ds, "TableA");
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    Console.WriteLine("ID={0},Name={1},CreateTime={2},ParantId={3}", dr[0], dr[1], dr[2], dr[3]);
                }
            }
        }

  

DataReaser

static void QueryDataByDataReader()
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                connection.Open();
                string sql = @"SELECT  [Id] ,
                                        [Name] ,
                                        [CreateTime] ,
                                        [ParantId]
                                FROM    [Test].[dbo].[TableA] WITH ( NOLOCK )";

                using (SqlCommand sqlCmd = connection.CreateCommand())
                {
                    sqlCmd.CommandText = sql;
                    SqlDataReader sqlDr = sqlCmd.ExecuteReader();
                    while (sqlDr.HasRows)//是否返回数据
                    {
                        while (sqlDr.Read())//从第一行开始顺序读取数据集到最后一行
                        {
                            Console.WriteLine("ID={0},Name={1},CreateTime={2},ParantId={3}", sqlDr[0].ToString(), sqlDr[1].ToString(), sqlDr[2].ToString(), sqlDr[3].ToString());
                        }
                    }
                }
            }
        }

  

DataTable

static void QuerybyDataTable()
        {
            SqlConnection connection = new SqlConnection(ConnectionString);
            connection.Open();
            string sql = @"SELECT  [Id] ,
                                        [Name] ,
                                        [CreateTime] ,
                                        [ParantId]
                                FROM    [Test].[dbo].[TableA] WITH ( NOLOCK )";
            SqlDataAdapter sqlda = new SqlDataAdapter(sql, ConnectionString);
            DataTable dt = new DataTable();
            sqlda.Fill(dt);
            foreach (DataRow item in dt.Rows)
            {
                Console.WriteLine("{0},{1},{2},{3}", item[0].ToString(), item[1].ToString(), item[2].ToString(),item[3].ToString());
            }
            connection.Close();
        }

  

 

posted @ 2017-06-23 15:43  一只大老鼠  阅读(260)  评论(0编辑  收藏  举报