使用sql连接sqlserver进行增删查改基本操作类

来源:互联网 发布:天敏智能网络机顶盒 编辑:程序博客网 时间:2024/06/05 18:21

整理以前写的代码,直接贴下

Sql_To_SqlServer类代码如下:

using System;using System.Collections.Generic;using System.Data.SqlClient;using System.Linq;using System.Text;using System.Threading.Tasks;namespace SQL_LINQ{    class Sql_To_SqlServer    {        private string strsql;        //连接数据库函数        public SqlConnection Connection()        {            string strcon =@"Data Source=服务器名称;                             Initial Catalog=数据库名称;Persist Security Info=True;                             User ID=登录名;Password=登陆密码";              //连接字符串            SqlConnection con = new SqlConnection(strcon);              //连接数据库            try            {                con.Open();             //打开连接                return con;            }            catch (Exception)            {                throw;            }                  }        //Select语句查询        public void Select()        {            strsql = "SELECT * FROM [dbo].[User]";               //SQL语句            SqlCommand cmd = new SqlCommand(strsql,Connection());               //初始化Command对象            SqlDataReader rd = cmd.ExecuteReader();             //初始化DataReader对象            while (rd.Read())            {                Console.WriteLine(rd["U_Name"].ToString());            }        }        //Insert Into语句插入        public void Insert(string s,string st)        {            strsql = "INSERT INTO [dbo].[User] (U_Name,U_Pwd) VALUES (@name1,@name2)";             //SQL语句            SqlCommand cmd = new SqlCommand(strsql,Connection());            cmd.Parameters.AddWithValue("@name1",s);                //添加参数,防止sql注入            cmd.Parameters.AddWithValue("@name2",st);            int n = cmd.ExecuteNonQuery();            Console.WriteLine("成功插入"+n+"条数据");        }        //Update语句更新        public  void Update()        {            strsql = "UPDATE [dbo].[User] SET U_Name='李四小号' WHERE U_Id=2";             //SQL语句            SqlCommand cmd = new SqlCommand(strsql, Connection());            int n = cmd.ExecuteNonQuery();            Console.WriteLine("成功更新" + n + "条数据");        }        //Delete删除语句        public void Delete()        {            string strsql = "DELETE FROM [dbo].[User] WHERE U_Id = 5";             //SQL语句            SqlCommand cmd = new SqlCommand(strsql, Connection());            int n = cmd.ExecuteNonQuery();            Console.WriteLine("成功删除" + n + "条数据");        }    }}

主函数测试代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using SQL_LINQ;using System.Data.SqlClient;namespace SQL_LINQ{    class Program    {        static void Main(string[] args)        {            //Sql操作            #region            Sql_To_SqlServer sts = new Sql_To_SqlServer();            //sts.Select(); //查询函数            sts.Insert("s","s"); //插入函数            //sts.Update(); //更新函数            //sts.Delete(); //删除函数            #endregion            Console.ReadLine();        }    }}


0 0
原创粉丝点击