using用法和SqlCommand认识

using三种用法

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection conn = null;
    //实现了IDisposable接口的对象,可以使用using进行资源管理(如:SqlDataAdapter、SqlDataReader、DataSet、SqlCommand)
    //using会在作用于结束的时候自动调用对象的dispose方法
    //1.
    using (SqlConnection conn = new SqlConnection("server=.;database=Login;uid=sa;pwd=coder"))
    {
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "sql 语句";
        cmd.Connection = conn;
        conn.Open();
        cmd.ExecuteScalar();
        //conn.Close();//关闭的是程序和数据库之间的连接,
        //conn.Dispose();//这个方法内部是调用了close方法的
    }
    //2.
    using (conn = new SqlConnection("连接字符串")) { }
    //3.
    conn = new SqlConnection("连接字符串");
    using (conn) { }
}

SqlCommand认识

 //两种方式功能一样
 SqlCommand cmd=new SqlCommand(); cmd.Connetion=conn; cmd.CommandText=""; 
 SqlCommand cmd = new SqlCommand("",conn);
//两种方式功能一样
SqlCommand cmd=new SqlCommand();cmd.Connection=conn;
SqlCommand cmd = conn.CreateCommand();
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值