How to add a new row to an existing table using c# sql server

后端 未结 4 690
长发绾君心
长发绾君心 2021-01-19 05:17

I need to write a program. A part of the program is to write to an sql database (.mdf). I had a lot of trouble trying to add a new row to my table (called: \"Data\"). Here i

相关标签:
4条回答
  • 2021-01-19 05:26

    Try instead to set

    dRow = new DataRow();
    

    instead of

    dRow = ds.Tables["Data"].NewRow();
    

    and change

    da.Update(ds, "Data");
    

    to

    da.Update(ds);
    
    0 讨论(0)
  • 2021-01-19 05:35

    Force the dataset to Accept changes ie add ds.Acceptchanges to your code

    0 讨论(0)
  • 2021-01-19 05:36

    Two things I'm seeing, you're not initializing your Dataset (ds) or SqlDataAdapter (da) in anyway (unless you're simply leaving that out for post simplification). Part of the initialization of the da will be giving it an actual sql command.

    0 讨论(0)
  • 2021-01-19 05:39

    You need an InsertCommand in your SqlDataAdapter.

    EDIT:

    Here's a quick example I whipped up. There are many others out there, but this should get you going. It assumes that you have a table (dbo.Foos) with two columns (Foo int, Bar nvarchar(50)).

    namespace DataAdapterSample
    {
        using System;
        using System.Data;
        using System.Data.SqlClient;
    
        class Program
        {
            static void Main(string[] args)
            {
                using (SqlConnection connection = new SqlConnection(@"Data Source=[your server];Initial Catalog=[your database];Integrated Security=true;"))
                {
                    using (SqlDataAdapter dataAdapter = new SqlDataAdapter())
                    {
                        dataAdapter.SelectCommand = new SqlCommand("select Foo, Bar from dbo.Foos", connection);
                        dataAdapter.InsertCommand = new SqlCommand("insert into dbo.Foos (Foo, Bar) values (@Foo, @Bar)", connection);
                        dataAdapter.InsertCommand.Parameters.Add(new SqlParameter("Foo", SqlDbType.Int, 4, "Foo"));
                        dataAdapter.InsertCommand.Parameters.Add(new SqlParameter("Bar", SqlDbType.NText, 50, "Bar"));
    
                        using (DataSet dataSet = new DataSet())
                        {
                            dataAdapter.Fill(dataSet);
    
                            Console.WriteLine("There are {0} rows in the table", dataSet.Tables[0].Rows.Count);
    
                            DataRow newRow = dataSet.Tables[0].NewRow();
                            newRow["Foo"] = 5;
                            newRow["Bar"] = "Hello World!";
                            dataSet.Tables[0].Rows.Add(newRow);
    
                            dataAdapter.Update(dataSet);
                        }                
    
                        //Just to prove we inserted
                        using (DataSet newDataSet = new DataSet())
                        {
                            dataAdapter.Fill(newDataSet);
                            Console.WriteLine("There are {0} rows in the table", newDataSet.Tables[0].Rows.Count);                
                        }                
                    }
                }
                Console.ReadLine();        
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题