C# 在WPF应用程序中执行插入查询时发生SqlCommand方法错误
我正在用C#编写WPF应用程序。数据库是SQLServer2008。我在数据库中有一个表“Employee”,我需要在其中插入一行。我已成功连接到数据库,但当我尝试执行此行时:C# 在WPF应用程序中执行插入查询时发生SqlCommand方法错误,c#,wpf,visual-studio-2010,sql-server-2008,C#,Wpf,Visual Studio 2010,Sql Server 2008,我正在用C#编写WPF应用程序。数据库是SQLServer2008。我在数据库中有一个表“Employee”,我需要在其中插入一行。我已成功连接到数据库,但当我尝试执行此行时: cmd = new SqlCommand(cmdText, conn); 出现此错误:“System.Data.SqlClient.SqlCommand.SqlCommand(String,System.Data.SqlClient.SqlConnection)”的最佳重载方法匹配有一些无效参数。 这是我的密码: pr
cmd = new SqlCommand(cmdText, conn);
出现此错误:“System.Data.SqlClient.SqlCommand.SqlCommand(String,System.Data.SqlClient.SqlConnection)”的最佳重载方法匹配有一些无效参数。这是我的密码:
private void addbtn_Click(object sender, RoutedEventArgs e)
{
//FUNCTION TO ADD NEW EMPLOYEE RECORD IN DATABASE
try
{
conn.ConnectionString = "Data Source=AZEEMPC;" + "Initial Catalog=IEPL_Attendance_DB;";
conn.Open();
cmdText = "INSERT INTO Employee VALUES ('" + strCurrentString + "','" + emp_name.Text + "')";
cmd = new SqlCommand(cmdText, conn);
data_ad = new SqlDataAdapter(cmd);
data = new DataSet();
data_ad.Fill(data);
MessageBox.Show("Record Inserted Successfully!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
有什么建议吗?
conn
需要是SqlConnection
类型-您能确认它是吗
SqlConnection conn;
因为它是本机SQL Server连接,所以不需要在连接字符串中传递驱动程序名称
conn.ConnectionString = "Server=AZEEMPC;Database=IEPL_Attendance_DB;Trusted_Connection=true;";
谢谢你的指点!我刚刚注意到我将conn声明为:OdbcConnection conn=newodbcconnection();现在我将其更改为:SqlConnection conn=newsqlconnection();但在更改此选项后,我的应用程序无法连接到数据库!!它显示“用户登录失败”异常。我已在windows身份验证模式下配置sql server。SqlConnection conn=new SqlConnection();我已编辑我的回答以响应在连接字符串中包含trusted_connection=true以启用可信的windows身份验证连接。