Friday, March 6, 2020

Multiple Connection String

Dear All,

In this post i have explain you multiple connection string

XML Connection String:-
protected void Button2_Click(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        StreamReader path = new StreamReader("D:\\myXmlData.xml");
        // string path = Server.MapPath("~/myXmlData.xml");
        ds.ReadXml(path);
        SqlConnection con = new SqlConnection(Tallystrsqlcon);
        SqlBulkCopy sbc = new SqlBulkCopy(con);
        sbc.DestinationTableName = "mtblUser";
        sbc.ColumnMappings.Add("Sno", "Sno");
        sbc.ColumnMappings.Add("UserId", "UserId");
        sbc.ColumnMappings.Add("Password", "Password");
        sbc.ColumnMappings.Add("UserName", "UserName");
        sbc.ColumnMappings.Add("Email", "Email");
        sbc.ColumnMappings.Add("GroupIn", "GroupIn");
        sbc.ColumnMappings.Add("UserType", "UserType");
        sbc.ColumnMappings.Add("EmpID", "EmpID");
        sbc.ColumnMappings.Add("Designation", "Designation");
        sbc.ColumnMappings.Add("SubGroup", "SubGroup");
        sbc.ColumnMappings.Add("ViewGroup", "ViewGroup");
        sbc.ColumnMappings.Add("Status", "Status");
        try
        {
            con.Open();
            sbc.WriteToServer(ds.Tables[0]);
            SqlCommand cmd = new SqlCommand("Select Sno, UserName,Designation from mtblUser", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds1 = new DataSet();
            da.Fill(ds1);
            GridView1.DataSource = ds1;
            GridView1.DataBind();


        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());

        }
        finally
        {
            System.Threading.Thread.Sleep(20000);
            con.Close();
           // Label1.Text = "Records Added Successfully !";
        }

    }

Fox Pro Connection String:-
<add name="strCon" connectionString="provider=VFPOLEDB.1 ;data source= 'C:\Program Files (x86)\Microsoft Visual FoxPro OLE DB Provider\Samples\Northwind';password='';user id=''"/>

OleDbConnection connection = new OleDbConnection(
    "Provider=VFPOLEDB.1;Data Source=.\\Northwind\\Northwind.dbc;"
);
connection.Open();
DataTable tables = connection.GetSchema(
    System.Data.OleDb.OleDbMetaDataCollectionNames.Tables
);

foreach (System.Data.DataRow rowTables in tables.Rows)
{
    Console.Out.WriteLine(rowTables["table_name"].ToString());
    DataTable columns = connection.GetSchema(
        System.Data.OleDb.OleDbMetaDataCollectionNames.Columns,
        new String[] { null, null, rowTables["table_name"].ToString(), null }
    );
    foreach (System.Data.DataRow rowColumns in columns.Rows)
    {
        Console.Out.WriteLine(
            rowTables["table_name"].ToString() + "." +
            rowColumns["column_name"].ToString() + " = " +
            rowColumns["data_type"].ToString()
        );
    }
}

Excel 2007:-
String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "D:\\Simple.xlsx" + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";
        OleDbConnection con = new OleDbConnection(constr);
        OleDbCommand oconn = new OleDbCommand("Select [EmpID],[EmpName],[Designation] from [Employee$]", con);
        con.Open();
        OleDbDataAdapter sda = new OleDbDataAdapter(oconn);

=======================================================
Thank you


No comments:

Post a Comment