[英]An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll Additional information: Keyword not supported: 'username'
[英]Exception thrown: 'System.ArgumentException' in System.Data.dll
我尝试了很多方法来解决这个问题,但没有找到正确的答案。 单击保存按钮时,会出现以下消息;
抛出异常:System.Data.dll 中的“System.ArgumentException”
并突出显示该行
公共 static SqlConnection con = new SqlConnection(s);
这是详细代码。
Connection.cs 文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Data;
namespace IMS
{
class Connection
{
private static string s = File.ReadAllText(Application.StartupPath + "\\Connect.txt");
public static SqlConnection con = new SqlConnection(s);
}
}
我在哪里编写插入代码以在表中插入数据
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Data;
namespace IMS
{
class insertion
{
public void InsertUser(string name, string username, string password, string email, Int16 status)
{
try
{
SqlCommand cmd = new SqlCommand("sy_InsertUser", Connection.con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@password", password);
cmd.Parameters.AddWithValue("@email", email);
cmd.Parameters.AddWithValue("@status", status);
Connection.con.Open();
if (cmd.ExecuteNonQuery()>= 1)
{
mainclass.ShowSucsMsg("User Added Successfuly");
}
Connection.con.Close();
}
catch (Exception ex)
{
Dashborad.ShowExpMsg(ex);
}
}
}
}
数据保存按钮代码
public override void savebtn_Click(object sender, EventArgs e)
{
insertion obj = new insertion();
obj.InsertUser(usnametxt.Text, usUsernametxt.Text, usPasswordtxt.Text, usEmailtxt.Text, Status);
}
应用程序配置
<appSettings>
<add key="" value="\\Connect.txt" />
</appSettings>
您不应将连接保留为 static。 您应该以这种方式使用连接:
using (var connection = new SqlConnection(
connectionString))
{
var command = new SqlCommand("sy_InsertUser", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@name", name);
command.Parameters.AddWithValue("@username", username);
command.Parameters.AddWithValue("@password", password);
command.Parameters.AddWithValue("@email", email);
command.Parameters.AddWithValue("@status", status);
command.connection.Open();
if (command.ExecuteNonQuery()>= 1)
{
mainclass.ShowSucsMsg("User Added Successfuly");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.