1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > asp.net的几种简单连接数据库方法

asp.net的几种简单连接数据库方法

时间:2021-08-13 20:44:18

相关推荐

asp.net的几种简单连接数据库方法

-------- 调用存储过程,连接控件参数,显示数据

SqlConnection conn = new SqlConnection();

conn.ConnectionString = "Data Source=.;Database=qdxt;Integrated Security=SSPI";

conn.Open();

SqlCommand comm = new SqlCommand();

comm.Connection = conn;

mandType = CommandType.StoredProcedure;

mandText = "proc_shiyanshishiyongliang";

SqlParameter pa = new SqlParameter("@sh_sh_id", SqlDbType.Char);

pa.Value = DropDownList1.SelectedItem.ToString();

comm.Parameters.Add(pa);

comm.Parameters.Add("@shiyongliang", SqlDbType.Int).Direction = ParameterDirection.Output;

comm.ExecuteScalar();

int i = (int)comm.Parameters["@shiyongliang"].Value;

TextBox1.Text = i.ToString();

------ 代码连接数据库查询结果

string y_name = TextBox1.Text;

string y_mima = TextBox2.Text;

string y_leixing=DropDownList1.Text;

SqlConnection conn = new SqlConnection();

conn.ConnectionString = "data source=.;database=qdxt;Integrated Security=SSPI";

conn.Open();

SqlCommand com = new SqlCommand();

com.Connection = conn;

mandText = "select * from yh where y_name=@y_name and y_mima=@y_mima and y_leixing=@y_leixing";

SqlParameter pa = new SqlParameter("@y_name", SqlDbType.VarChar,10);

pa.Value = y_name;

com.Parameters.Add(pa);

pa = new SqlParameter("@y_mima", SqlDbType.VarChar, 10);

pa.Value = y_mima;

com.Parameters.Add(pa);

pa = new SqlParameter("@y_leixing", SqlDbType.VarChar,10);

pa.Value = y_leixing;

com.Parameters.Add(pa);

SqlDataReader re = com.ExecuteReader();

if (re.Read())

if (y_leixing == "guanliyuan")

Response.Redirect("kcanpai.aspx");

else

Response.Redirect("新文件夹1/chuqinchaxun.aspx");

else

Response.Write("<script language='javascript'>alert('密码或用户名错误');</script>");

-----------代码连接数据库增删改

SqlConnection conn = new SqlConnection();

conn.ConnectionString = "data source=.;database=qdxt;Integrated Security=SSPI";

conn.Open();

SqlCommand com = new SqlCommand();

com.Connection = conn;

mandText = "insert into dbo.kc(kc_id,kc_name) values(@kc_id,@kc_name)";

SqlParameter pa = new SqlParameter("@kc_id", SqlDbType.NChar, 10);

pa.Value = TextBox1.Text;

com.Parameters.Add(pa);

pa = new SqlParameter("@kc_name", SqlDbType.VarChar, 10);

pa.Value = TextBox2.Text;

com.Parameters.Add(pa);

int i = com.ExecuteNonQuery();

if (i != 0)

{

Response.Write("<script language='javascript'>alert('添加数据成功');</script>");

}

else

{

Response.Write("<script language='javascript'>alert('添加数据失败');</script>");

}

Response.Redirect("kcshezhi.aspx");

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。