string strCon = "Data Source=xxx;Database=db;Uid=xx;Pwd=xx";
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection(strCon);
connection.Open();
string username = Environment.UserName;
string sql = "select 語句";
SqlCommand sqlcom = new SqlCommand(sql, connection);
string n = sqlcom.ExecuteScalar().ToString ();
if (n!="0")
{
Response.Redirect("頁面");
}
}
1、返回update、delete、insert的行数
int rows = cmd.ExecuteNonQuery();
这段代码展示了如何使用C#连接SQL数据库并执行查询。通过`SqlConnection`打开连接,设置SQL语句,并使用`SqlCommand`的`ExecuteScalar`方法获取SELECT语句返回的第一行第一列的数据。如果返回值不等于'0',则重定向到特定页面。
1290

被折叠的 条评论
为什么被折叠?



