使用C#插入SQL Server数据库的问题

迈克尔·阿格霍尔姆

我正在尝试将数据插入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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

SQL Server数据库连接问题

来自分类Dev

SQL Server数据库连接问题

来自分类Dev

将整数和字符串插入SQL Server数据库C#的问题

来自分类Dev

批量插入SQL Server 2012数据库-C#

来自分类Dev

如何使用C#将数组的元素插入SQL Server数据库?

来自分类Dev

还原数据库的 SQL Server 2012 登录问题

来自分类Dev

使用sql命令将数据插入数据库的问题

来自分类Dev

使用C#还原SQL Server数据库

来自分类Dev

使用PHP将SQL插入数据库-INSERT INTO问题

来自分类Dev

C#无法将数据插入SQL Server数据库

来自分类Dev

C#无法将数据插入SQL Server数据库

来自分类Dev

从DataTable插入Sql Server数据库

来自分类Dev

将数据插入数据库的问题

来自分类Dev

无法从C#将inkPicture插入SQL Server数据库

来自分类Dev

如何使用fileupload插入数据库sql server

来自分类Dev

使用PDO将映像插入SQL Server数据库

来自分类Dev

使用sqlzoo数据库的SQL问题

来自分类Dev

使用C#将数据插入SQLite数据库

来自分类Dev

在C#中插入SQL数据库

来自分类Dev

从SQL Server数据库项目引用可移植类库的问题

来自分类Dev

从SQL Server数据库项目引用可移植类库的问题

来自分类Dev

如何在不使用数据库向导的情况下使用C#将数据插入SQL Server(Visual Studio)

来自分类Dev

如何在不使用数据库向导的情况下使用C#将数据插入SQL Server(Visual Studio)

来自分类Dev

使用C#将Null插入数据库

来自分类Dev

从C#使用Entity Framework插入数据库

来自分类Dev

如何使用C#将图片插入数据库?

来自分类Dev

如何使用C#插入INTO访问数据库

来自分类Dev

SQL server 的数据库是 sql 数据库吗?

来自分类Dev

Oracle数据库查询问题-插入数据问题

Related 相关文章

热门标签

归档