2014-03-31 59 views
1
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(); 

    } 

你好!使用C將一條記錄添加到數據庫中#

我對insert命令有問題。我收到以下錯誤:

'game_id'附近的語法不正確。

是列存在於數據庫中。我得到了同樣的錯誤爲我的更新功能,但我沒有貼在這裏,因爲我相信它必須來自相同的錯誤來源。我有2個數據網格,一個是父母,另一個是孩子。基本上我想在子表中添加新記錄。

預先感謝您!

+1

看看實體框架。它可以爲你生成所有這些樣板代碼。 – turtlepick

回答

2

INSERT語法是完全錯誤的 - 你應該使用:

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

首先:我建議總是明確指定要插入的列名單。

其次:VALUES需要在其將插入的值列表周圍有括號

並在下一次 - 查找優秀,免費提供 SQL Server文檔第一!這種完全基本的東西可以是容易在網上找到 - 例如,有關INSERT語法can be found here on Technet

該文檔是很徹底的細節,它顯示了所有的細節,所有的選項,使用它的所有不同的方式,它有大量的示例代碼,太 - 請使用文檔之前在這裏提出這樣的基本問題(使用文檔和查找東西是部分是開發者,畢竟!) - 謝謝。

相關問題