command用法
来源:互联网 发布:linux deploy使用教程 编辑:程序博客网 时间:2024/05/19 16:49
一、SqlCommand常见的属性:SqlCommand属性为执行命令作准备。
1、 CommandText属性:执行的SQL语句;
2、 Connection属性:连接数据库SqlConnection对象;
3、 CommandType属性:解析CommandText的值;
SqlCommandcmd = new SqlCommand("login", conn);
cmd.CommandType =CommandType.StoredProcedure; //这里采用存储过程
4、 CommandTimeout属性:设置需要执行多久停止;
5、 Parameters属性:设置参数;
二、SqlCommand类构造函数
SqlCommandmyCommand = new SqlCommand(sqlupdate, conn);
三、SqlCommand常见的方法: SqlCommand方法主要执行SQL语句。
1、 ExecuteReader()方法:主要执行select语句。将结果返回到SqlDataReader对象。
例:
SqlCommandmyconn = new SqlCommand("select * from v_economy2_comidd where eid="+ Request.QueryString["eid"] + "", conn);
conn.Open();
SqlDataReaderrd = myconn.ExecuteReader();
rd.Read();
Lbyear1.Text = rd["year1"].ToString();
Lbmonth1.Text = rd["month1"].ToString();
Lbcom_name.Text = rd["com_name"].ToString();
rd.Close();
conn.Close();
或者:
while(rd.Read())
{
}
2、 ExecuteNonQuery()方法:主要执行Insert、Update、Delete语句。返回值为该命令所影响的行数。
例:
protectedvoid Button1_Click(objectsender, EventArgs e)
{
stringclass_name = TextBox1.Text;
stringpwd = PwdMd5.md5l("111");
SqlCommandmyconn = new SqlCommand("insert into UserAdmin(UserName,UserPwd,UserLevel,tim,num)values('"+ class_name + "','" + pwd + "','U',@tim,1)", conn);
myconn.Parameters.Add(newSqlParameter("@tim",SqlDbType.DateTime, 8));
myconn.Parameters["@tim"].Value =DateTime.Now.ToString(); //显示详细的日期和时间
conn.Open();
myconn.ExecuteNonQuery();
conn.Close();
Response.Write("<script language='javascript'>alert('添加管理员成功!初始密码为123456');location='AdUserMag.aspx'</script>");
}
protectedvoid GridView1_RowUpdating(objectsender, GridViewUpdateEventArgse)
{
stringsqlUpdate = "update UserAdmin set UserPwd=@UserPwd Where UserId='"+ int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString().Trim()) +"'";
SqlCommandMyConn = new SqlCommand(sqlUpdate, conn);
MyConn.Parameters.Add(newSqlParameter("@UserPwd",SqlDbType.VarChar, 500));
MyConn.Parameters["@UserPwd"].Value =PwdMd5.md5l("111");
conn.Open();
MyConn.ExecuteNonQuery();
conn.Close();
Response.Write("<script language='javascript'>alert('还原密码成功!!还原密码为111');location='AdUserMag.aspx'</script>");
}
protectedvoid GridView1_RowDeleting(objectsender, GridViewDeleteEventArgse)
{
stringsqldel = "delete from UserAdmin where UserId="+ int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString().Trim());
SqlCommandmyconn = new SqlCommand(sqldel, conn);
conn.Open();
myconn.ExecuteNonQuery();
//lbsql.Text = "<b>已删除记录</b><br>" + sqldel;
conn.Close();
BindGrid();
}
3、 ExecuteScalar()方法:返回获得的聚合值(共有多少行数据)。
string strSql;
strSql="select name from user;
sqlCommand comm =new sqlCommand(strSql,conn);
object so=comm.ExecuteScalar();
Response.write(so.ToString());
4、 ExecuteXmlReader()方法
- command用法
- FTP command相关用法
- Command 的基本用法
- ns2中command的用法
- OLE DB Command transformation 用法
- C# 中Command 基本用法
- visual studio的command window用法
- mac系统Command键几个小用法
- Vim Global Command用法(g命令)
- Command
- Command
- Command
- command
- Command
- Command
- command
- command!!!
- command
- IBM Lotus集成发短信的功能
- 一个你可能不知道的cuda细节
- 数据库中参数的使用
- 【C语言】语法--extern
- 对话框实例
- command用法
- CubicTest集成测试
- 了解 uri, content provide,包括 data android:host,android:scheme(转
- CheckBox实例
- STL容器学习总结
- vs2010与vtk配置
- 构造函数(惑)
- An example code in .net of AWS EC2
- 天龙八部与珍珑(文/《北京青年报》记者 郭婷婷)