none
How to save data from untyped dataset to database in c#? RRS feed

  • Question

  • hi

    I have a table called "CUSTOMERS" with a column "CUST_ID" inside a untyped dataset. Now i want to save my data to database through the table inside a untyped dataset. How to save this?

    Is this ok : dsCust.Tables["Customers"].Rows[0]["cust_id"] = textbox1.Text ;

    If is what is the procedure to save data to database?

    Thanks in advance

    Monday, May 3, 2010 12:40 PM

Answers

  • Hi AristocratP,

    You need to make a connection to SQL .

    Here is a demo which demonstrates how to open a connection,how to create a command and how to read the result using dataReader in SQL Server:

           .....      
          using System.Data.SqlClient;

          class dataReaderExample
          {
              public static void Main()
              {
                   SqlConnection Cn = new SqlConnection("Data Source = .;Database = WorkNotes;Uid = sa;Pwd  = ;");     
                   SqlCommand Cmd  = Cn.CreateCommand();
                   Cmd.CommandText = "select * from rrv where id=(SELECT MAX(id)  FROM rrv";
                   Cn.Open();
                   SqlDataReader Rdr = Cmd.ExecuteReader();
                   while (Rdr.Read())
                   {
                         //fill in the textbox you want.                  
                   }
                   Rdr.Close();
                   Cn.Close();
               }

           }

    Best wishes,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Monday, May 10, 2010 2:09 AM
    Thursday, May 6, 2010 9:17 AM

All replies

  • Hi,

    One way is to iterate through all the fields of row(s) and fill list of parameters. Once parameters are ready, pass these to Command object - which can work with either a stored procedure or text query. Use ExecuteScalar or ExecuteNonQuery method of ADO.NET to perform the db operation.

    Here is sample code:

    DbCommand command = null;

                DbConnection connection = null;

                DbTransaction transaction = null;

     

                try

                {             

                        command = SqlDatabase.GetStoredProcCommand(SQL_PART_INSERT);

                        SqlDatabase.AddOutParameter(command, PARTID, DbType.Int32, 0);             

                    SqlDatabase.AddInParameter(command, COMPANYID, DbType.Int32, table.rows[0]["ColumnName"]);

     

                    connection = SqlDatabase.CreateConnection();

                    connection.Open();

                    transaction = connection.BeginTransaction(IsolationLevel.Serializable);

     

                    SqlDatabase.ExecuteNonQuery(command, transaction);               

                    transaction.Commit();

     

                }

                catch (Exception ex)

                {

                    if (transaction != null)

                    {

                        transaction.Rollback();

                    }               

                }

                finally

                {

                    if (connection != null && connection.State == ConnectionState.Open)

                    {

                        connection.Close();

                    }

                }


     


    Asim
    Monday, May 3, 2010 1:27 PM
  • Aristocart,

    Actually you have to generate suitable INSERT, DELETE and UPDATE command for that table TableAdapter. it's not impossible but is hard to do and time consuming. so, I strongly recommend you using Microsoft Visual Studio itself for data connection which generate needed codes automatically. please see http://msdn.microsoft.com/en-us/library/ms171886(VS.80).aspx (Connecting to Data in Visual Studio) and save your time by using Microsoft technologies.

    Good luck,

    Yasser.


    Don't be stickler and wine with William Shakespeare after the solution :^)
    "And this our life, exempt from public haunt, finds tongues in trees, books in the running brooks, sermons in stones, and good in everything." William Shakespeare
    Monday, May 3, 2010 1:29 PM
  • Hi AristocratP,

    You need to make a connection to SQL .

    Here is a demo which demonstrates how to open a connection,how to create a command and how to read the result using dataReader in SQL Server:

           .....      
          using System.Data.SqlClient;

          class dataReaderExample
          {
              public static void Main()
              {
                   SqlConnection Cn = new SqlConnection("Data Source = .;Database = WorkNotes;Uid = sa;Pwd  = ;");     
                   SqlCommand Cmd  = Cn.CreateCommand();
                   Cmd.CommandText = "select * from rrv where id=(SELECT MAX(id)  FROM rrv";
                   Cn.Open();
                   SqlDataReader Rdr = Cmd.ExecuteReader();
                   while (Rdr.Read())
                   {
                         //fill in the textbox you want.                  
                   }
                   Rdr.Close();
                   Cn.Close();
               }

           }

    Best wishes,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Monday, May 10, 2010 2:09 AM
    Thursday, May 6, 2010 9:17 AM
  • @AristocratP - Your example shows how to SELECT data. The question asks about how to SAVE data. Please can you try to expand your answer to include how the SAVE data? Thanks. -- Mark Kamoski

    -- Mark Kamoski

    Friday, August 23, 2019 3:06 PM
  • Ok, but the question is -- how does one SAVE data?

    -- Mark Kamoski

    Friday, August 23, 2019 3:08 PM
  • Ok, but the question is -- how does one SAVE data?

    -- Mark Kamoski

    Here is a pattern to following to get the basics down

    https://github.com/karenpayneoregon/MasterDetailsSqlServerNorthWind/blob/master/DataOperations/Operations.cs#L133


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, August 24, 2019 1:53 AM
    Moderator