繁体   English   中英

使用C#将记录添加到数据库

[英]Adding a record to the database using C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace lab1
{
    public partial class Form1 : Form
{

    DataSet ds = new DataSet(); //holding place in memory
    SqlDataAdapter daParent = new SqlDataAdapter();
    SqlDataAdapter daChild = new SqlDataAdapter();
    SqlConnection cs = new SqlConnection("Data Source=user-PC\\SQLEXPRESS; Initial Catalog=dede;Integrated Security=TRUE");

    BindingSource ParentBS = new BindingSource();
    BindingSource ChildBS = new BindingSource();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        SqlCommand slctParent = new SqlCommand("select * from Developerss", cs); //select statement
        daParent.SelectCommand = slctParent; // attaching sql command into the select command property of the data adapter Parent
        daParent.Fill(ds, "Developerss"); //fullfilling the data set, calling the table Parent

        SqlCommand slctChild = new SqlCommand("select * from Games", cs);
        daParent.SelectCommand = slctChild; 
        daParent.Fill(ds,"Games");

        dgParent.DataSource = ds.Tables["Developerss"];
        dgChild.DataSource = ds.Tables["Games"];
        dgParent.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        dgChild.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//click anywhere in the table we have the whole row selected

        ParentBS.DataSource = ds.Tables[0];
        ChildBS.DataSource = ds.Tables[1];

        textId.DataBindings.Add(new Binding("Text",ChildBS,"game_id"));
        textName.DataBindings.Add(new Binding("Text", ChildBS, "game_name"));
        textPlatform.DataBindings.Add(new Binding("Text", ChildBS, "game_platform"));
        textDeveloperId.DataBindings.Add(new Binding("Text", ParentBS, "d_id"));


    }

    private void dgParent_SelectionChanged(object sender, EventArgs e)
    {
        ds.Tables["Games"].DefaultView.RowFilter = "developer_id = " + dgParent.CurrentRow.Cells["d_id"].Value;
        //whenever the selection change on the dgparent use the value of the current row that we're on
        dgChild.DataSource = ds.Tables["Games"];
    }

    private void addBtn_Click(object sender, EventArgs e)
    {
        daChild.InsertCommand = new SqlCommand("insert into Games values @id, @name, @platform, @developerId", cs);
        daChild.InsertCommand.Parameters.Add("@id", SqlDbType.Int).Value = textId.Text;
        daChild.InsertCommand.Parameters.Add("@name", SqlDbType.VarChar).Value = textName.Text;
        daChild.InsertCommand.Parameters.Add("@platform", SqlDbType.VarChar).Value = textPlatform.Text;
        daChild.InsertCommand.Parameters.Add("@developerId", SqlDbType.Int).Value = ds.Tables[0].Rows[ParentBS.Position][0];

        cs.Open();
        daChild.InsertCommand.ExecuteNonQuery();
        cs.Close();

    }

你好!

我的insert命令有问题。 我收到以下错误:

'game_id'附近的语法不正确。

是的,该列存在于数据库中。 我的更新功能遇到相同的错误,但我没有在此发布,因为我认为它必须来自相同的错误源。 我有2个数据网格,一个是父级,另一个是子级。 基本上我想在子表中添加新记录。

先感谢您 !

您的INSERT语法完全错误-您应该使用:

INSERT INTO dbo.Games(Co1, Col2, ..., ColN)
VALUES (@id, @name, @platform, @developerId)

首先:我建议始终明确指定要插入的列列表

其次: VALUES需要在要插入的值列表周围有括号

下一次- 请首先查看出色的免费 SQL Server文档! 完全可以在网上轻松找到这样的基本内容-例如, 可以在Technet上找到有关INSERT语法的详细信息

该文档非常详尽,它显示了所有详细信息,所有选项,使用它的各种方式,并且还包含大量示例代码- 请在使用之前先问该文档,然后再问这些基本问题(使用文档并查找)。毕竟,这是成为开发人员的一部分 !)-谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM