我真的在搜索所有内容和所有地方,但无论如何,无论我尝试过什么,我的这段代码都不会起作用。 所以我在SQL服务器上创建了一个数据库并将其链接,测试它,非常有用。好的,问题是当我试图在网站文本框中插入数据并将数据复制/传输到我的数据库表中时。所以我现在只得到一个错误,说'Exception unhandeld并且必须声明标量变量“@f_name”。 如果有人可以提供帮助,我将不胜感激?感谢。
private void Save(string hfname, string hlname, string hemail, string hcomment)
{
SqlConnection myConn = new SqlConnection(GetConnectionString());
String sql = "INSERT INTO helpdesk (First_Name, Last_Name, Email, Comments) VALUES " + " (@f_name, @l_name, @email, @comment)";
SqlCommand cmd = new SqlCommand(sql, myConn);
ITDBDataset itdbDataSet = new ITDBDataset();
SqlDataAdapter dataAdapter;
try
{
myConn.Open();
dataAdapter = new SqlDataAdapter(cmd);
dataAdapter.Fill(itdbDataSet);
myConn.Close();
SqlParameter[] param = new SqlParameter[6];
//para,[0]
param[0] = new SqlParameter("@f_name", System.Data.SqlDbType.VarChar, 50);
param[1] = new SqlParameter("@l_name", System.Data.SqlDbType.VarChar, 50);
param[2] = new SqlParameter("@email", System.Data.SqlDbType.VarChar, 30);
param[3] = new SqlParameter("@comment", System.Data.SqlDbType.VarChar, 600);
param[0].Value = hfname;
param[1].Value = hlname;
param[2].Value = hemail;
param[3].Value = hcomment;
cmd.CommandType = System.Data.CommandType.Text;
cmd.ExecuteNonQuery();
//SqlDataReader reader = cmd.ExecuteReader();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Not Added, check context!";
msg += ex.Message;
throw new Exception(msg);
}
//finally
//{
// myConn.Close();
//}
}
对可能出错的任何建议?
答案 0 :(得分:0)
您忘记将参数添加到命令中,并在调用方法之前关闭连接。我建议你使用:
private void Save(string hfname, string hlname, string hemail, string hcomment)
{
SqlConnection myConn = new SqlConnection(GetConnectionString());
String sql = "INSERT INTO helpdesk (First_Name, Last_Name, Email, Comments) VALUES " + " (@f_name, @l_name, @email, @comment)";
SqlCommand cmd = new SqlCommand(sql, myConn);
ITDBDataset itdbDataSet = new ITDBDataset();
SqlDataAdapter dataAdapter;
try
{
myConn.Open();
dataAdapter = new SqlDataAdapter(cmd);
dataAdapter.Fill(itdbDataSet);
SqlParameter[] param = new SqlParameter[6];
//para,[0]
param[0] = new SqlParameter("@f_name", System.Data.SqlDbType.VarChar, 50);
param[1] = new SqlParameter("@l_name", System.Data.SqlDbType.VarChar, 50);
param[2] = new SqlParameter("@email", System.Data.SqlDbType.VarChar, 30);
param[3] = new SqlParameter("@comment", System.Data.SqlDbType.VarChar, 600);
param[0].Value = hfname;
param[1].Value = hlname;
param[2].Value = hemail;
param[3].Value = hcomment;
cmd.Parameters.AddRange(param);//add the parameters
cmd.CommandType = System.Data.CommandType.Text;
cmd.ExecuteNonQuery();
//SqlDataReader reader = cmd.ExecuteReader();
myConn.Close();
}
答案 1 :(得分:0)
我建议你使用AddWithValue例如
SqlConnection myConn = new SqlConnection(GetConnectionString());
String sql =
"INSERT INTO helpdesk (First_Name, Last_Name, Email, Comments) VALUES " +
" (@f_name, @l_name, @email, @comment)";
SqlCommand cmd = new SqlCommand(sql, myConn);
cmd.Parameters.AddWithValue("@f_name",hfname);
cmd.Parameters.AddWithValue("@l_name",hlname);
cmd.Parameters.AddWithValue("@email",hemail);
cmd.Parameters.AddWithValue("@comment",hcomment);
ITDBDataset itdbDataSet = new ITDBDataset();
SqlDataAdapter dataAdapter;
try
{
myConn.Open();
dataAdapter = new SqlDataAdapter(cmd);
dataAdapter.Fill(itdbDataSet);
myConn.Close();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Not Added, check context!";
msg += ex.Message;
throw new Exception(msg);
}