我正在尝试将数据插入C#中的SQL Server数据库中。
我是编程新手,并且想学习正确的编程方法。
到目前为止,我有一个数据库类:
public SqlConnection connection()
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "DESKTOP-UPVVOJP";
builder.InitialCatalog = "Lagersystem";
builder.IntegratedSecurity = true;
return new SqlConnection(builder.ToString());
}
产品分类文件:
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int ProductStock { get; set; }
public int ProductCategoryID { get; set; }
public int ProductEmployeeID { get; set; }
public DateTime ProductCreatedDate { get; set; }
// Constructor
public Product(string productname, int productstock, int productcategoryid, int productemployeeid)
{
ProductName = productname;
ProductStock = productstock;
ProductCategoryID = productcategoryid;
ProductEmployeeID = productemployeeid;
ProductCreatedDate = DateTime.Now;
}
}
在我的程序中插入方法:
static void InsertProduct(string productname, int productstock, int productcategoryid, int productemployeeid)
{
Database db = new Database();
SqlConnection conn = db.connection();
using (SqlCommand command = new SqlCommand(@"INSERT INTO Products (ProductName, ProductStock, ProductCategoryID, ProductEmployeeID, ProductCreatedDate)
VALUES('{0}', {1}, {2}, {3}, '{4}')", conn))
{
string formattet = string.Format(productname, productstock, productcategoryid, productemployeeid, DateTime.Now);
Console.WriteLine(formattet);
conn.Open();
command.ExecuteNonQuery();
conn.Close();
}
}
和我的主要方法:
static void Main(string[] args)
{
while (true)
{
Console.Write("Indtast Produktnavn :");
string productname = Console.ReadLine();
Console.Write("Hvor mange er der på lager? : ");
int productstock = int.Parse(Console.ReadLine());
Console.Write("Hvilken kategori ID hører produktet til? : ");
int productcategoryid = int.Parse(Console.ReadLine());
Console.Write("Hvilket medarbejder ID har oprettet produktet? : ");
int productemployeeid = int.Parse(Console.ReadLine());
try
{
InsertProduct(productname, productstock, productcategoryid, productemployeeid);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
}
我的数据库具有以下设置:
CREATE TABLE Products
(
ProductID int PRIMARY KEY IDENTITY(1,1) NOT NULL,
ProductName nvarchar(32) NOT NULL,
ProductStock INT,
ProductCategoryID INT,
ProductEmployeeID int NOT NULL,
ProductCreatedDate datetime NOT NULL DEFAULT GETDATE()
);
通过运行此命令,我可以输入数据“ productname,stock,categoryid,employeeid”
作为“ Jacket,10,1,2”,这应该可以工作,因为我有一个ID为1的类别和一个ID为2的员工。
运行程序时,出现错误
'1'附近的语法不正确
我的猜测是这方面的一些错误:
VALUES('{0}', {1}, {2}, {3}, '{4}')"
我建议使用查询参数,如下所示:
conn.Open();
SqlCommand command = new SqlCommand(
@"INSERT INTO Products
(ProductName, ProductStock, ProductCategoryID, ProductEmployeeID, ProductCreatedDate)
VALUES(@ProductName, @ProductStock, @ProductCategoryID, @ProductEmployeeID, getDate())",
conn
);
command.Parameters.Add("@ProductName", productname);
command.Parameters.Add("@ProductStock", productstock);
command.Parameters.Add("@ProductCategoryID", productcategoryid);
command.Parameters.Add("@ProductEmployeeID", productemployeeid);
command.ExecuteNonQuery();
conn.Close();
请注意,您可以使用SQL Server函数直接在查询中计算当前日期/时间,getDate()
而不是通过c#传递它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句