【发布时间】:2022-03-01 08:06:23
【问题描述】:
我是 C# 到 SQL 交互的新手,如果这里的问题很明显或重复,我深表歉意。我正在尝试在“Clientes”表中插入一个新行,此代码的第一部分连接到数据库,然后检查表中是否存在重复项,我知道这些部分有效。我包括它以防万一问题可能来自我的连接字符串或其他东西。
一旦到达 Try Catch,它就会抛出我输入的“错误”消息,所以我知道插入时发生了故障。
通常我可以根据错误消息中的信息解决这样的问题,但这只是给我
[Exception throw: 'System.Data.dll in System.Data.SqlClient.SqlException']
在输出选项卡中,错误列表中没有任何内容,也找不到更多详细信息,并且我无法根据类似的 SO 帖子推断出问题。
if ( textBox_tel.Text.All(c => char.IsDigit(c))) //checks no letters in phone number
{
string connectionstring;
SqlConnection con;
connectionstring = @"Data Source = DRAGONSLAYER;Initial Catalog=bancodb;User id=bancodb_admin;Password=admin";
con = new SqlConnection(connectionstring);
con.Open(); //now connected to the DB
string querysignupsubmitcheck = "Select * from Clientes Where Login = '" + textBox_usr.Text + "'";
SqlDataAdapter sda_signupsubmitcheck = new SqlDataAdapter(querysignupsubmitcheck, con);
DataTable dtbl_signupsubmitcheck = new DataTable();
sda_signupsubmitcheck.Fill(dtbl_signupsubmitcheck);
con.Close();
if (dtbl_signupsubmitcheck.Rows.Count < 1) //checks the new client row isn't a duplicate
{
try
{
string querysignupsubmit = "Insert into Clientes (Nombre, Telefono, Login, Password) Values (" +
textBox_name.Text + ", " +
textBox_tel.Text + ", " +
textBox_usr.Text + ", " +
textBox_pword2.Text + ")";
SqlCommand sc_signupsubmitc = new SqlCommand(querysignupsubmit, con);
sc_signupsubmitc.ExecuteNonQuery();
this.Close();
objform_login.Show();
}
catch { label_alert.Text = "ERROR DE BASE DE DATOS"; }
}
else
{
label_alert.Text = "usuario ya existe";
}
}
else
{
label_alert.Text = "Telefono acepta solo numeros";
}
根据another question here 上的建议,我将 try-catch 语句中的代码更改为此,但它仍然抛出相同的异常:
using (con)
{
string querysignupsubmit = "INSERT INTO Clientes (Nombre, Telefono, Login, Password) VALUES (@val1, @val2, @val3, @val4)";
using (SqlCommand sc_signupsubmit = new SqlCommand())
{
sc_signupsubmit.Connection = con;
sc_signupsubmit.CommandText = querysignupsubmit;
sc_signupsubmit.Parameters.AddWithValue("@val1", textBox_name.Text);
sc_signupsubmit.Parameters.AddWithValue("@val1", textBox_tel.Text);
sc_signupsubmit.Parameters.AddWithValue("@val1", textBox_usr.Text);
sc_signupsubmit.Parameters.AddWithValue("@val1", textBox_pword2.Text);
con.Open();
sc_signupsubmit.ExecuteNonQuery();
con.Close();
this.Close();
objform_login.Show();
}
}
感谢任何帮助或建议,这是我要插入的表的代码:
CREATE TABLE [dbo].[Clientes] (
[ClienteID] INT IDENTITY (1, 1) NOT NULL,
[Nombre] VARCHAR (255) NOT NULL,
[Telefono] VARCHAR (20) NOT NULL,
[Login] VARCHAR (255) DEFAULT ('default_login') NOT NULL,
[Password] VARCHAR (128) NOT NULL,
CONSTRAINT [PK_Clientes] PRIMARY KEY CLUSTERED ([ClienteID] ASC)
);
编辑: 这是完整的输出和错误列表选项卡,退出消息来自我关闭它
EDIT2:我很笨,多次声明 Val1,很笨。感谢大家的帮助。
【问题讨论】:
-
具体异常的详细信息会很有用。如果你在catch上断点,然后查看细节会有帮助。
-
您需要从异常中获取更多信息。异常的 type 并不能告诉你太多。您还想从异常、任何内部异常、任何可选的附加数据等中获取错误消息。但主要的是错误消息,它试图告诉您出了什么问题。跨度>
-
将此
catch { label_alert.Text = "ERROR DE BASE DE DATOS"; }更改为catch(Exception ex){ label_alert.Text = "ERROR DE BASE DE DATOS"; }并在大括号内放置一个断点以获取有关实际异常的更多信息。 -
顺便说一句:不要以纯文本形式存储密码!
-
还要仔细查看您的行中带有
AddWithValue的行 - 我希望错误消息类似于“参数@val2 是预期但未提供”
标签: c# sql visual-studio system.data.sqlclient