实现留言板把新的数据插入到数据库中的操作,编译提示“只有 assignment、call、increment、decrement 和 new 对象表达式可用作语句”,错误语句是 "dbConn.Open; "和“dbConn.Close;”
部分代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
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();
//插入数据库
string strSql = "Insert Into post(name,subject,message,[date],ip) values( ' " + strName + " ', ' " + strSubject + " ', ' " + strMsg + " ', ' " + strDate + " ', ' " + strIP + " ') ";
this.ExcuteSql(strSql);
Response.Write( "OK! ");
}
}
private void ExcuteSql(string sql)
{
string strConn = "Data Source=localhost;Initial Catalog=Msgboard;UserID=sa;Password=123456; ";
DbProviderFactory dbProviderFactory = DbProviderFactories.GetFactory( "System.Data.SqlClint ");
DbConnection dbConn = dbProviderFactory.CreateConnection();
dbConn.ConnectionString = strConn;
dbConn.Open;
DbCommand dbComm = dbProviderFactory.CreateCommand();
dbComm.Connection = dbConn;
dbComm.CommandText = sql;
dbComm.ExecuteNonQuery();
dbConn.Close;
dbConn.Dispose();