This example will read a csv file into a dataset and save it back when you press button 1

//This example code is from eran.rivlis at gmail.com

DataTable dt = new DataTable();

private void Form1_Load(object sender, EventArgs e)
{
string conString = @”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:csv” +
@”;Extended Properties=””Text;HDR=No;FMT=Delimited”””;
OleDbConnection conn = new OleDbConnection(conString);
OleDbDataAdapter da = new OleDbDataAdapter(@”Select * from table1.csv”, conn);
da.Fill(dt);
dataGridView1.DataSource = dt;
}

private void button1_Click(object sender, EventArgs e)
{
StringBuilder sbCSV = new StringBuilder();
int intColCount = dt.Columns.Count;
foreach (DataRowView dr in dt.DefaultView)
{
for (int x = 0; x < intColCount; x++) { sbCSV.Append(dr[x].ToString()); if ((x + 1) != intColCount) { sbCSV.Append(","); } } sbCSV.Append(" "); } using (StreamWriter sw = new StreamWriter(@"c:csv able1.csv")) { sw.Write(sbCSV.ToString()); } } [/csharp]

Create table through SqlConnection

   

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

   class CommandExampleCreateDb
   {
      static void Main() 
      {
         SqlConnection thisConnection = new SqlConnection("server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI");
         SqlCommand nonqueryCommand = thisConnection.CreateCommand();

         try 
         {
            thisConnection.Open();

            nonqueryCommand.CommandText = "CREATE DATABASE MyDb";
            Console.WriteLine(nonqueryCommand.CommandText);

            nonqueryCommand.ExecuteNonQuery();
            Console.WriteLine("Database created, now switching");
            thisConnection.ChangeDatabase("MyDb");

            nonqueryCommand.CommandText = "CREATE TABLE MyJava2sTable (COL1 integer)";
            Console.WriteLine(nonqueryCommand.CommandText);
            Console.WriteLine("Number of Rows Affected is: {0}", nonqueryCommand.ExecuteNonQuery());

            nonqueryCommand.CommandText = "INSERT INTO MyJava2sTable VALUES (99)";
            Console.WriteLine(nonqueryCommand.CommandText);
            Console.WriteLine("Number of Rows Affected is: {0}", nonqueryCommand.ExecuteNonQuery());
         
         } catch (SqlException ex) {
         
            Console.WriteLine(ex.ToString());
         
         } finally {  
         
            thisConnection.Close();
            Console.WriteLine("Connection Closed.");
         
         }
      }
   }



           
          


Use ExecuteNonQuery() to run DDL statements: create table

   
 

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

class ExecuteDDL {
    public static void Main() {
        SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");

        SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
        mySqlCommand.CommandText =
          "CREATE TABLE MyPersons (" +
          "  PersonID int CONSTRAINT PK_Persons PRIMARY KEY," +
          "  FirstName nvarchar(15) NOT NULL," +
          "  LastName nvarchar(15) NOT NULL," +
          "  DateOfBirth datetime" +
          ")";

        mySqlConnection.Open();
        Console.WriteLine("Creating MyPersons table");
        int result = mySqlCommand.ExecuteNonQuery();
        Console.WriteLine("mySqlCommand.ExecuteNonQuery() = " + result);
        mySqlCommand.CommandText =
          "ALTER TABLE MyPersons " +
          "ADD EmployerID nchar(5) CONSTRAINT FK_Persons_Customers " +
          "REFERENCES Customers(CustomerID)";

        result = mySqlCommand.ExecuteNonQuery();
        Console.WriteLine("mySqlCommand.ExecuteNonQuery() = " + result);
        mySqlCommand.CommandText = "DROP TABLE MyPersons";
        result = mySqlCommand.ExecuteNonQuery();
        Console.WriteLine("mySqlCommand.ExecuteNonQuery() = " + result);
        mySqlConnection.Close();
    }
}

    


use the ExecuteNonQuery() method to run DDL statements

   


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

class ExecuteDDL
{
  public static void Main()
  {
    SqlConnection mySqlConnection =new SqlConnection("server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");

    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();

    mySqlCommand.CommandText =
      "CREATE TABLE MyEmployee (" +
      "  ID int CONSTRAINT PK_Persons PRIMARY KEY," +
      "  FirstName nvarchar(15) NOT NULL," +
      "  LastName nvarchar(15) NOT NULL," +
      "  DateOfBirth datetime" +
      ")";

    mySqlConnection.Open();

    Console.WriteLine("Creating MyEmployee table");
    int result = mySqlCommand.ExecuteNonQuery();
    Console.WriteLine("mySqlCommand.ExecuteNonQuery() = " + result);

    mySqlCommand.CommandText =
      "ALTER TABLE MyEmployee " +
      "ADD EmployerID nchar(5) CONSTRAINT FK_Persons_Customers " +
      "REFERENCES Employee(ID)";

    Console.WriteLine("Altering MyEmployee table");
    result = mySqlCommand.ExecuteNonQuery();
    Console.WriteLine("mySqlCommand.ExecuteNonQuery() = " + result);

    mySqlCommand.CommandText = "DROP TABLE MyEmployee";

    Console.WriteLine("Dropping MyEmployee table");
    result = mySqlCommand.ExecuteNonQuery();
    Console.WriteLine("mySqlCommand.ExecuteNonQuery() = " + result);

    mySqlConnection.Close();
  }
}


           
          


Create database through SqlConnection

   


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

   class CommandExampleCreateDb
   {
      static void Main() 
      {
         SqlConnection thisConnection = new SqlConnection("server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI");
         SqlCommand nonqueryCommand = thisConnection.CreateCommand();

         try 
         {
            thisConnection.Open();

            nonqueryCommand.CommandText = "CREATE DATABASE MyDb";
            Console.WriteLine(nonqueryCommand.CommandText);

            nonqueryCommand.ExecuteNonQuery();
            Console.WriteLine("Database created, now switching");
            thisConnection.ChangeDatabase("MyDb");

            nonqueryCommand.CommandText = "CREATE TABLE MyJava2sTable (COL1 integer)";
            Console.WriteLine(nonqueryCommand.CommandText);
            Console.WriteLine("Number of Rows Affected is: {0}", nonqueryCommand.ExecuteNonQuery());

            nonqueryCommand.CommandText = "INSERT INTO MyJava2sTable VALUES (99)";
            Console.WriteLine(nonqueryCommand.CommandText);
            Console.WriteLine("Number of Rows Affected is: {0}", nonqueryCommand.ExecuteNonQuery());
         
         } catch (SqlException ex) {
         
            Console.WriteLine(ex.ToString());
         
         } finally {  
         
            thisConnection.Close();
            Console.WriteLine("Connection Closed.");
         
         }
      }
   }


           
          


SqlConnection connection string

   


    using System;
    using System.Data.SqlClient;
  
  class ConnectToSqlConnection {
    static void Main(string[] args)  {
      String sConn = "server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";

        String sSQL = "select id, firstname, lastname from Employee";

      SqlConnection oConn = new SqlConnection(sConn);
      oConn.Open();

      SqlCommand oCmd = new SqlCommand(sSQL, oConn);
      SqlDataReader oReader = oCmd.ExecuteReader();

      int idxID = oReader.GetOrdinal("id");
      int idxFirstName = oReader.GetOrdinal("firstname");
      int idxLastName = oReader.GetOrdinal("lastname");

      while(oReader.Read()) {
        Console.WriteLine("{0} {1} {2}",
          oReader.GetValue(idxID),
          oReader.GetValue(idxFirstName),
          oReader.GetValue(idxLastName));
      }
    }
  }


           
          


Ole db connection string for SQL Server

   

using System;
using System.Data.OleDb;

public class ReadFromOleDb
{
  [STAThread]
  static void Main(string[] args)
  {
    String sConn = "provider=sqloledb;server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
    
    String sSQL = "select id, firstname, lastname from Employee";

    OleDbConnection oConn = new OleDbConnection(sConn);
    oConn.Open();

    OleDbCommand oCmd = new OleDbCommand(sSQL, oConn);
    OleDbDataReader oReader = oCmd.ExecuteReader();

    int idxID = oReader.GetOrdinal("id");
    int idxFirstName = oReader.GetOrdinal("firstname");
    int idxLastName = oReader.GetOrdinal("lastname");

    while(oReader.Read()) {
      Console.WriteLine("{0} {1} {2}",
        oReader.GetValue(idxID),
        oReader.GetValue(idxFirstName),
        oReader.GetValue(idxLastName));
    }
  }
}