Argumentexception未处理..如何解决? [英] Argumentexception was unhandled.. How to fix ?
问题描述
SqlConnection con = new SqlConnection("data source=SAMIRAN-PC; Initial catalog = DB2; Integral security= true");
SqlDataAdapter dat = new SqlDataAdapter();
dat.InsertCommand = new SqlCommand(" INSERT INTO TAB2 VALUES(@NAME,@AGE)", con);
dat.InsertCommand.Parameters.Add("@NAME",SqlDbType.VarChar).Value = textBox1.Text;
dat.InsertCommand.Parameters.Add("@AGE", SqlDbType.VarChar).Value = textBox2.Text;
con.Open();
dat.InsertCommand.ExecuteNonQuery();
con.Close();
}
在第一行说它就像这样的错误。我找不到任何错误。仍然会发生。
如果任何人都可以做,将非常欢迎!!
谢谢!!
我尝试过:
可能存在连接问题。我我正在这样做。但我不知道哪种连接是最好和最正确的。帮助!
at the first line it says like such error.Nd I cannot find any thing wrong. Still it is happening.
If any one can do, will be most welcome !!
Thank You !!
What I have tried:
There may be connection problem.I am doing with such way. But I don't know which is the best and correct way to connect. Help !
推荐答案
您的连接字符串不正确。更新为:
Your connection string is not correct. Update it to:
Data Source=SAMIRAN-PC; Initial Catalog = DB2; Integrated Security= SSPI
此连接字符串指定服务器(数据源),数据库(初始目录)并提及Windows身份验证应该是使用(综合安全)。在你的代码中,最后一个参数不正确。
This connection string specifies the server (data source), database (intial catalog) and mentions that windows authentication should be used (integrated security). In your code, last parameter is not correct.
我会尝试替换:
I would try to replace:
dat.InsertCommand.Parameters.Add("@AGE", SqlDbType.VarChar).Value = textBox2;
with:
with :
dat.InsertCommand.Parameters.Add("@AGE", SqlDbType.VarChar).Value = textBox2.text;
您可能还需要转换 textBox2.text
改为数字。
这篇关于Argumentexception未处理..如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!