2014-07-07 63 views
1

我在更新C#中的数据库时出错。这里是代码:在数据库中更新时出错

string connectionstring = "server=AMAN;database=student;Integrated Security=True"; 
SqlConnection conn; 
string Admission_no = txtAddmissionNo.Text; 
SqlCommand cmd; 
conn = new SqlConnection(connectionstring); 
conn.Open(); 
string query = "update fees set [email protected]_fee, [email protected]_fee,[email protected]_fee ,[email protected]_money,[email protected]_fee,[email protected]_fee,[email protected]_fair,[email protected]_fee ,[email protected],[email protected]_fee,[email protected]_fine,[email protected]_dues,[email protected]_feemonth ,[email protected]_fee,[email protected]_fee,[email protected],[email protected],[email protected] where [email protected]_no"; 
cmd=new SqlCommand(query,conn); 
cmd.Parameters.AddWithValue("@Admission_no", Admission_no); 
cmd.Parameters.AddWithValue("@prospectues_fee", prospectues_fee); 
cmd.Parameters.AddWithValue("@registration_fee", registration_fee); 
cmd.Parameters.AddWithValue("@admission_fee", admission_fee); 
cmd.Parameters.AddWithValue("@security_money", security_money); 
cmd.Parameters.AddWithValue("@misslaneous_fee", misslaneous_fee); 
cmd.Parameters.AddWithValue("@development_fee", development_fee); 
cmd.Parameters.AddWithValue("@transport_fair", transport_fair); 
cmd.Parameters.AddWithValue("@computer_fee", computer_fee); 
cmd.Parameters.AddWithValue("@activity", activity); 
cmd.Parameters.AddWithValue("@hostel_fee", hostel_fee); 
cmd.Parameters.AddWithValue("@dely_fine", dely_fine); 
cmd.Parameters.AddWithValue("@back_dues", back_dues); 
cmd.Parameters.AddWithValue("@tution_fee", tution_fee); 
cmd.Parameters.AddWithValue("@other_fee", other_fee); 
cmd.Parameters.AddWithValue("@total", total); 
cmd.Parameters.AddWithValue("@tution_feemonth", tution_feemonth); 
cmd.Parameters.AddWithValue("@deposit", deposit_fee); 
cmd.Parameters.AddWithValue("@dues", dues); 

cmd = new SqlCommand(query, conn); 

try 
{ 
    cmd.ExecuteNonQuery(); 
} 
catch (Exception ex) 
{ 
    MessageBox.Show(ex.ToString()); 
} 

错误是@prospectues_fee标量必须声明,我已经声明。

+1

btw;有几个'IDisposable'对象,应该真的使用'使用'... –

+0

对不起,它不工作 – Aman

+1

“对不起,它不工作”; a:**完全**哪个“it”不工作,以及b:“不工作”是什么意思,**完全**? –

回答

4

的错误是简单的比我想象:

cmd = new SqlCommand(query, conn); 
... // lots of code 
cmd = new SqlCommand(query, conn); 
try 
{ 
    cmd.ExecuteNonQuery(); 
} 
catch (Exception ex) 
{ 
    MessageBox.Show(ex.ToString()); 
} 

您正在创建一个第二命令只是在执行之前;这第二个命令有文本但没有参数。删除第二行new SqlCommand一行。


这听起来像可怕的null VS DBNull问题。参数中的null的意思是“不要发送这个”。这真的很愚蠢,但我们确实很愚蠢。试着用:

cmd.Parameters.AddWithValue("@prospectues_fee", 
    ((object)prospectues_fee) ?? DBNull.Value); 

现在重复了所有的参数......或者只是添加遍历它们并检查它们的方法:

static void FixTheCrazy(DbCommand command) { 
    foreach(DbParameter param in command.Parameters) { 
     if(param.Value == null) param.Value = DBNull.Value; 
    } 
} 

另外,使用像dapper,会做一个工具它适合你:

using(varconn = new SqlConnection(connectionstring)) 
{ 
    conn.Execute(query, new { 
     Admission_no, prospectues_fee, registration_fee, ... 
     deposit_fee, dues }); 
}