运行时提示@a1必需声名。
请各位帮看看错在哪里,如果不是这样写,那应该如何用类和"@变量"来操作呢?谢谢了!!!
有两个类文件件,分别为Classconnection.cs、Classcommand.cs和一个index.aspx文件,其中index.aspx有三个textbox和一个button控件。
代码如下:
Classconnection.cs中的代码如下
//////////////////////////////////
using System;
using System.Data;
using System.Configuration;
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;
/// <summary>
/// Classconnection 的摘要说明
/// </summary>
public class Classconnection
{
protected IDbConnection dbconn;
public string dataType;
public Classconnection()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public IDbConnection dbconnection()
{
switch(dataType)
{
case "SQL":
dbconn = new System.Data.SqlClient.SqlConnection("server=localhost;uid=sa;pwd=sa;database=fsxdata");
break;
case "OLEDB":
dbconn =new System.Data.OleDb.OleDbConnection("");
break;
}
return dbconn;
}
}
Classcommand.cs的代码如下
///////////////////////////////
using System;
using System.Data;
using System.Configuration;
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;
/// <summary>
/// Classcommand 的摘要说明
/// </summary>
public class Classcommand
{
private IDbConnection dbconnect;
public IDbCommand dbcommand;
private IDataReader dbRead;
Classconnection dbconns = new Classconnection();
public Classcommand()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void dbopen(string str)
{
dbconns.dataType = str;
dbconnect = dbconns.dbconnection();
dbconnect.Open();
dbcommand = dbconnect.CreateCommand();
}
public void dbclose()
{
dbcommand.Dispose();
dbconnect.Close();
dbconnect.Dispose();
}
public int dbinsert(string ss)
{
dbcommand.CommandText = ss;
return dbcommand.ExecuteNonQuery();
}
}
index.aspx的代码如下
///////////////////////////
protected void Button1_Click(object sender, EventArgs e)
{
Classcommand cmd = new Classcommand();
cmd.dbopen("SQL");
cmd.dbinsert("insert into dd(fsx1,fsx2,fsx3) values(@a1,@a2,@a3)");
cmd.dbcommand.Parameters.Add(new SqlParameter("@a1",SqlDbType.Char,10));
cmd.dbcommand.Parameters["@a1"].value= TextBox1.Text;
cmd.dbcommand.Parameters.Add(new SqlParameter("@a2", SqlDbType.Char, 10));
cmd.dbcommand.Parameters["@a2"].value = TextBox2.Text;
cmd.dbcommand.Parameters.Add(new SqlParameter("@a3", SqlDbType.Char, 10));
cmd.dbcommand.Parameters["@a3"].value = TextBox3.Text;
cmd.dbclose();
Response.Write("<script>alert('添加成功!');</script>");
}