C# 将产品插入数据库时出现问题
我正在努力学习如何使用C 我想使用C# 将产品插入数据库时出现问题,c#,sql-server,sql-insert,C#,Sql Server,Sql Insert,我正在努力学习如何使用C 我想使用System.Data.SqlClient和SqlConnectionStringBuilder将产品添加到数据库中 目前我有一个数据库类: public SqlConnection connection() { SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder.DataSource = "DESKTOP-UPVVOJP"
System.Data.SqlClient
和SqlConnectionStringBuilder
将产品添加到数据库中
目前我有一个数据库类:
public SqlConnection connection()
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "DESKTOP-UPVVOJP";
builder.InitialCatalog = "Lagersystem";
builder.IntegratedSecurity = true;
// Console.WriteLine(builder);
return new SqlConnection(builder.ToString());
}
产品类别:
public int ProductID { get; set; }
public string ProductName { get; set; }
public DateTime ProductCreatedDate { get; set; }
在我的程序中,我尝试通过以下方法添加产品:
static void AddProduct(string name)
{
Database db = new Database();
SqlConnection conn = db.connection();
try
{
using (SqlCommand command = new SqlCommand("INSERT INTO Products VALUES(@ProductName, @ProductCreatedDate)", conn))
{
command.Parameters.Add(new SqlParameter("@ProductName", name));
command.Parameters.Add(new SqlParameter("ProductCreatedDate", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
conn.Open();
command.ExecuteNonQuery();
conn.Close();
Console.WriteLine(name + " Tilføjet");
}
}
catch (ArgumentException e)
{
Console.WriteLine(e);
Console.ReadKey();
}
}
我从main调用此方法:
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("INPUT ProductName :");
string input = Console.ReadLine();
string name = input.ToString();
try
{
AddProduct(name); // Add the data to the SQL database.
Console.WriteLine("Product created!");
}
catch
{
Console.WriteLine("Input error");
Console.ReadKey();
}
}
}
当我调试我的程序时,我可以跟踪我输入的输入贯穿整个程序,一直到AddProduct(name)代码>方法。那里的一切似乎都很好
但不知何故,它并没有添加任何内容,我尝试的每一件事情最终都会出现在catch块Console.WriteLine(“输入错误”)代码>
这里有人能帮我了解一下我哪里出了问题,或者是什么让我在来这里的路上失败了
如果有帮助,我的数据库有一个表Products
,如下所示:
ProductID - int - primary key - Identity(1, 1) - no null
ProductName - nvarchar(32) - no null
ProductCreatedDate - Datetime - no null
非常感谢,现在可以用了
似乎我不能将我的使用包装在我的连接上,这样做会导致连接到数据库的错误
static void AddProduct(string name)
{
Database db = new Database();
SqlConnection conn = db.connection();
try
{
using (SqlCommand command = new SqlCommand("INSERT INTO Products VALUES(@ProductName, @ProductCreatedDate)", conn))
{
command.Parameters.Add(new SqlParameter("@ProductName", name));
command.Parameters.Add(new SqlParameter("@ProductCreatedDate", DateTime.Now));
conn.Open();
command.ExecuteNonQuery();
conn.Close();
Console.WriteLine(name + " Tilføjet");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
不要ToString
日期-直接插入。不要编写无用的try/catch。Catch有一个参数Catch(Exception ex),然后在传递的ex中有许多关于错误的信息。至少尝试写出ex.Message属性。我认为这个错误是由于您将DateTime变量作为字符串传递造成的。AddWithValue采用类型passes来生成参数,但如果数据库列需要日期,则传递字符串通常会导致第二个错误点。在连接周围放置using语句。这是保留非托管资源并需要尽快释放的主对象。ExecuteOnQuery返回一个整数,指示更改的行数。当您有主键时,每个键都必须是唯一的。当您执行插入并且密钥已经存在时,您将从execute返回一个零,并且必须使用Update。当您执行更新且密钥不存在时,您还将得到一个返回的零,然后必须使用Insert。您的第二个参数在添加到命令时缺少@
。例如,您可以使用catch(exception ex)
捕获异常,然后打印ex.Message
,这可能会给您一个提示,而不是只打印input error
,出了什么问题……如果您在自我回答中发布了改进的代码,这可能会对其他程序员有所帮助您已经发布了这么多,但您从未真正发布过错误消息。如果没有错误信息,请不要提问,更不要说回答问题
static void Main(string[] args)
{
//TryCreateTable();
DisplayProducts();
while (true)
{
Console.WriteLine("INPUT ProductName :");
string input = Console.ReadLine();
string name = input.ToString();
try
{
AddProduct(name); // Add the data to the SQL database.
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}