SqlCommand  用法

admin   ·   发表于 4个月前   ·   C sharp

SqlCommand  这个对象可以让我们在数据库上做一下操作,比如说增、删、改、查。都可以使用SqlCommand  这个对象。

首先,要使用SqlCommand  对象的话,必须先声明它。

SqlCommand cmd = new SqlCommand(strSQL, conn);


其中strSQL 是我们定义好的SQL 语句,conn 是声明好的数据库链接。

我们来看一下如果需要在数据库中插入一条数据应该怎么操作,也就是insert。

一般的可以用ExecuteNonQuery() 方法;ExecuteNonQuery() 方法还可以执行修改、删除语句。

string strSQL = @"intsert into tb_users (id,username,password ) values ('1','system','123456')";
SqlCommand cmd = new SqlCommand(strSQL , conn);
int i =  Convert.ToInt32(cmd.ExecuteNonQuery());


ExecuteNonQuery() 方法执行update 语句。

 
string strSQL= @"update tb_users set password='654321' where username='system'";
SqlCommand cmd = new SqlCommand(strSQL);
cmd.Connection = conn;
int i =  Convert.ToInt32(cmd.ExecuteNonQuery());

ExecuteNonQuery() 方法执行delete 语句。 


string strSQL= @"delete from tb_users  where username='system'";
SqlCommand cmd = new SqlCommand(strSQL);
cmd.Connection = conn;
int i =  Convert.ToInt32(cmd.ExecuteNonQuery());


然后就是最常用的select 查询语句,

ExecuteReader()方法可以执行查找语句,它返回的是一个结果集,一般的我们可以用一个叫做SqlDataReader的对象来接收这个结果集,比如说想要查找所有的users ,下面的语句执行后,在data里面就得到了所有的tb_users 表里的信息了。

SqlCommand cmd = new SqlCommand("select * from tb_users", conn);
SqlDataReader data= cmd.ExecuteReader();


还有就是ExecuteScalar() 方法,它返回一个单一的结果。

             string sqlStr = "SELECT count(1) FROM tb_Users WHERE username='" + username +"' and password='"+password+"' ";
            Console.WriteLine(sqlStr); 
            conn = new SqlConnection(strSQLconn);
            conn.Open(); 
            SqlCommand comm = new SqlCommand(sqlStr,conn);
            int i = Convert.ToInt32(comm.ExecuteScalar().ToString());
            if(i>=1) {
                conn.Close();
                return true;
            }
            conn.Close();
            return false;

 

0 条回复   |  直到 4个月前 | 134 次浏览
登录后才可发表内容