save data into SQL database

get data from variable and save into database

Page 1 of 1

9 Replies - 14044 Views - Last Post: 02 August 2010 - 06:05 AM Rate Topic: -----

#1 programmerjiwa  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 29-June 10

save data into SQL database

Posted 01 August 2010 - 09:03 PM

i have done a simple CSV parser project..i write coding to read text files and view the data into data grid view...
Now i need to get that data save into SQL database table(table name=DataDB)...how i can make it?
thanks in advance

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;

namespace myTextReader
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog1.InitialDirectory = txtOpenFile.Text.Trim();
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    txtOpenFile.Text = openFileDialog1.FileName.Trim();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            try
            {

                if (string.IsNullOrEmpty(txtOpenFile.Text.Trim()))
                {
                    MessageBox.Show("Please select file");
                    return;
                }
                System.IO.StreamReader sr = new System.IO.StreamReader(txtOpenFile.Text.Trim());
                string inputLine = "";

                DataTable dt = new DataTable();
                dt.Columns.Add("Barcode");
                dt.Columns.Add("Price");
                dt.Columns.Add("QTY");
                dt.Columns.Add("Location");
                dt.Columns.Add("Pallet");
                dt.Columns.Add("UserID");
                dt.Columns.Add("Document");

                DataRow row;
                while ((inputLine = sr.ReadLine()) != null)
                {
                    string[] array;
                    array = inputLine.Split(',');
                    row = dt.NewRow();
                    row["Barcode"] = array[0];
                    row["Price"] = array[1];
                    row["QTY"] = array[2];
                    row["Location"] = array[3];
                    row["Pallet"] = array[4];
                    row["UserID"] = array[5];
                    row["Document"] = array[6];

                    dt.Rows.Add(row);
                }

                dt.Rows.RemoveAt(0);
                dataGridView1.DataSource = dt;

                sr.Close();
            }
            catch (Exception)
            {
                throw;
            }
        }




This post has been edited by eclipsed4utoo: 02 August 2010 - 04:56 AM
Reason for edit:: the code tags are [code] your code here [/code]


Is This A Good Question/Topic? 0
  • +

Replies To: save data into SQL database

#2 programmerjiwa  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 29-June 10

Re: save data into SQL database

Posted 01 August 2010 - 09:52 PM

.......!How to save data into SQL Table..
Was This Post Helpful? 0
  • +
  • -

#3 programmerjiwa  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 29-June 10

Re: save data into SQL database

Posted 02 August 2010 - 12:25 AM

hello everyone....
can someone tell me how to save data into SQL using C#...
thanks
Was This Post Helpful? 0
  • +
  • -

#4 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: save data into SQL database

Posted 02 August 2010 - 12:33 AM

1. Choose your database provider
2. Initialize your database connection
3. Initialize your SQL command
4. Open connection
5. Execute command
6. Close connection
7. Dispose connection and command objects (optional)


That's it...
Was This Post Helpful? 0
  • +
  • -

#5 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: save data into SQL database

Posted 02 August 2010 - 12:36 AM

I believe you asked this question in another thread already... What database provider are you using anyway? Microsoft SQL Server (Express) or any other?
Was This Post Helpful? 0
  • +
  • -

#6 programmerjiwa  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 29-June 10

Re: save data into SQL database

Posted 02 August 2010 - 12:52 AM

SQL Server Express
Was This Post Helpful? 0
  • +
  • -

#7 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: save data into SQL database

Posted 02 August 2010 - 12:57 AM

Google is your friend... This is what MSDN says:

private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
    }
}


Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: save data into SQL database

Posted 02 August 2010 - 05:02 AM

and a quick example of using a query with parameters(since you will need to do this)

using (SqlConnection cn = new SqlConnection(myConnectionString))
{
    using (SqlCommand cmd = cn.CreateCommand())
    {
        string sql = "INSERT INTO Table1 (Field1, Field2, Field3) VALUES (@Field1, @Field2, @Field3)";
        cmd.CommandText = sql;
        cmd.CommandType = CommandType.Text;

        cmd.Parameters.AddWithValue("@Field1", "value1");
        cmd.Parameters.AddWithValue("@Field2", "value2");
        cmd.Parameters.AddWithValue("@Field3", "value3");

        cn.Open();
  
        cmd.ExecuteNonQuery();
    }
}


Was This Post Helpful? 0
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: save data into SQL database

Posted 02 August 2010 - 05:48 AM

No need to post this twice. You have an answer in your other topic.

Also, please learn to have patience. Members are here on their own. They aren't required to be here 24/7. If somebody has an answer for you and they are here, they will give it to you.

There is no need to create multiple topics or bump your other topic just because it hasn't been answered in 45 minutes.

This post has been edited by eclipsed4utoo: 02 August 2010 - 05:48 AM

Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: save data into SQL database

Posted 02 August 2010 - 06:05 AM

Merged duplicate topics.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1