C#一次连接数据库执行多条sql语句(三种方法)

 

第一种方法:

            string str="server=.;uid=sa;pwd=111111;database=text_db";//连接字符串

            SqlConnection SCON = null;//连接对象

            SqlCommand SCom=new SqlCommand();//命令对象

            SCON=new SqlConnection(str);

            SCON.Open();

            SCom.CommandText = "insert into cum(nm,age,sex,addr) values(‘”+name1+”’, ‘”+age1+”’, ‘”+sex1”’,’”+addr1+”’); insert into cum(nm,age,sex,addr) values(‘”+name1+”’, ‘”+age1+”’, ‘”+sex1”’,’”+addr1+”’)";

            SCom.Connection = SCON;

            SCom.CommandType = CommandType.Text;

            int num = SCom.ExecuteNonQuery();

            SCON.Close();

            注意:这就是用分号隔开,把多条sql语句写成了一条sql语句来执行,因为没有用参数,可能有sql注入风险。

第二种方法:

            string str="server=.;uid=sa;pwd=111111;database=text_db";//连接字符串

            SqlConnection SCON;

            SqlCommand SCom1=new SqlCommand();//要执行sql语句的第一个命令对象

            SqlCommand SCom2=new SqlCommand();//要执行sql语句的第二个命令对象

            SCON=new SqlConnection(str);

            SCON.Open();

            //第一条要执行的sql语句

            SCom1.CommandText = "insert into cum(nm,age,sex,addr) values(@name,@age,@sex,@addr)";

            SCom1.Parameters.Add("@name", SqlDbType.Text);

            SCom1.Parameters.Add("@age", SqlDbType.Int);

            SCom1.Parameters.Add("@sex", SqlDbType.Bit);

            SCom1.Parameters.Add("@addr", SqlDbType.Text);

            SCom1.Parameters["@name"].Value = name1;

            SCom1.Parameters["@age"].Value = age1;

            SCom1.Parameters["@sex"].Value = sex1;

            SCom1.Parameters["@addr"].Value = addr1;

            SCom1.Connection = SCON;

            SCom1.CommandType = CommandType.Text;

            int num = SCom1.ExecuteNonQuery();

            //第二条要执行行的sql语句

            SCom2.CommandText = "insert into cum(nm,age,sex,addr) values(@name,@age,@sex,@addr)";

            SCom2.Parameters.Add("@name", SqlDbType.Text);

            SCom2.Parameters.Add("@age", SqlDbType.Int);

            SCom2.Parameters.Add("@sex", SqlDbType.Bit);

            SCom2.Parameters.Add("@addr", SqlDbType.Text);

            SCom2.Parameters["@name"].Value = name2;

            SCom2.Parameters["@age"].Value = age2;

            SCom2.Parameters["@sex"].Value = sex2;

            SCom2.Parameters["@addr"].Value = addr2;

            SCom2.Connection = SCON;

            SCom2.CommandType = CommandType.Text;

            int num = SCom2.ExecuteNonQuery();

            SCON.Close();

            这种方法就是利用了一个SqlConnection对象进行一次连接,在关闭前执行多个SqlCommand命令对象,执行完后再关闭连接。

第三种方法:

       

        //这里用事务来执行多条语句

        public string InsertAll(string[] cmdtext)
        {

            //打开连接
            Open();
            DbTransaction myTrans;
            DbCommand mycmd;
            if (IsAccess())
            {
                 mycmd =(OleDbCommand) Sqlcon.CreateCommand();
            }
            else
            {
                 mycmd =(SqlCommand) Sqlcon.CreateCommand();
            }

            myTrans = Sqlcon.BeginTransaction(IsolationLevel.ReadCommitted);
            mycmd.Connection = Sqlcon;
            mycmd.Transaction = myTrans;
            try
            {
                for (int i = 0; i < cmdtext.Length; i++)
                {
                    mycmd.CommandText = cmdtext[i];
                    mycmd.ExecuteNonQuery();
                }
                myTrans.Commit();
                return "1";
            }
            catch (Exception)
            {
                if (myTrans != null)
                    myTrans.Rollback();
                return "0";
            }
            finally
            {

                //关闭连接
                //Sqlcon.Close();
                if (Sqlcon != null)
                    Close();
            }
        }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值