usercontrolexample.ascx.cs
来自「水晶报表详细资料水晶报表详细资料水晶报表详细资料」· CS 代码 · 共 57 行
CS
57 行
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class UserControlExample : System.Web.UI.UserControl
{
protected void LoginButton_Click(object sender, EventArgs e)
{
SqlConnection mycon = new SqlConnection("server=(local);Database=fangdawei;Uid=sa;Pwd=");
mycon.Open();
SqlCommand mycom = new SqlCommand("select count(*) from Users where username='" + usernameTextBox.Text + "'", mycon);
int intcount = Convert.ToInt32(mycom.ExecuteScalar());
if (intcount > 0)
{
Response.Write("<script>alert('该用户名已经存在!')</script>");
}
else
{
SqlConnection con = new SqlConnection("server=(local);Database=fangdawei;Uid=sa;Pwd=");
con.Open();
//通过SqlConnection的BeginTransaction方法创建名为st的对象Transaction
SqlTransaction st = con.BeginTransaction();
SqlCommand com = con.CreateCommand();
//将SqlTransaction对象分配给SqlCommand对象的Transaction属性
com.Transaction = st;
try
{
//向用户表中插入注册信息
com.CommandText = "insert into Users(username,password,nick,email)values('" + usernameTextBox.Text + "','" + passwordTextBox.Text + "','" + nickTextBox.Text + "','" + emailTextBox.Text + "')";
com.ExecuteNonQuery();
//执行SQL语句
com.ExecuteNonQuery();
//提交事物
st.Commit();
Response.Write("<script>alert('注册成功!')</script>");
}
catch (Exception error)
{
Response.Write(error.ToString());
//回滚事物
st.Rollback();
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?