asp.net如何在SQL中插入数据

来源:百度知道 编辑:UC知道 时间:2024/05/15 12:30:24
是一个简单的留言本,用SQL数据库连接。
要将strname,strstrSubject,strMsg, strDate, strIP的值插入到数据库(表名是post)中,应该如何编写后面的代码?

string strname = this.tbName.Text;
string strSubject = this.tbSubject.Text;
string strMsg = this.tbMsg.Text;
string strIP = Request.UserHostAddress.ToString();
string strDate = System.DateTime.Now.ToString();
//插入数据库
SqlConnection strconn = new SqlConnection();
strconn.ConnectionString = "server=localhost" + "Database=post";
string strSql = "Insert Into post(name,subject,message,[date],ip) values(strname,strSubject,strMsg, strDate, strIP )";
……
Response.Redirect("Default.aspx");

以上的代码也有错的,请大虾指正!!!
@usosr

还是不能插入到数据库中,难道别的地方还要加代码的吗?我是新手~

strconn.ConnectionString = "server=localhost;Database=post;uid=;pwd=";

string strSql = "Insert Into post(name,subject,message,[date],ip) values("+strname+","+strSubject+","+strMsg+", "+strDate+", "+strIP+")";

楼上不对,字符形要有单引号的,数字行的不必,所以应该这样改
strconn.ConnectionString = "server=localhost;Database=post;uid=;pwd=";

string strSql = "Insert Into post(name,subject,message,[date],ip) values('"+strname+"','"+strSubject+"','"+strMsg+"', '"+strDate+"', '"+strIP+"')";

如果能用存储过程,效果会更好,建议楼主买一本入门ASP.NET的书,很多的,网上就可以下载,有问题去www.csdn.net问吧,那里国内的高手如云