Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
string source = "PORT=9000;DRIVER=Tally ODBC Driver;SERVER={(local)}";
            OdbcConnection con = new OdbcConnection(source);
            con.Open();
            DataTable dt = new DataTable();
            string[] restrictions = new string[1];
            dt = con.GetSchema("Tables");
            Database db = new Database();

            DropDownList1.DataSource = dt;
            DropDownList1.DataTextField = "Table_Name";
            DropDownList1.DataValueField = "Table_Name";
            DropDownList1.DataBind();

i get tally schema from this but i want to use this schema and create database in sql server 2008 using,
kindly help me,
if my question is not understanding then ask me again.
thanx.
Posted
Updated 5-Jul-18 0:32am
v3

1 solution

Step 1: Create a static method which will take datatable as parameter and extract all coulmns, its datatype. Then it generates sql quries. Here is the method:
C#
public static string CreateTABLE(DataTable table)
{
    string sqlQuery;
    sqlQuery = "CREATE TABLE " + table.TableName + "(";
    for (int i = 0; i < table.Columns.Count; i++)
    {
        sqlQuery += "\n [" + table.Columns[i].ColumnName + "] ";
        string columnType = table.Columns[i].DataType.ToString();

        // You will get different data type in  C#(you need to change it SQLServer):Boolean,Byte,Char,DateTime,Decimal,Double,Int16,Int32,Int64,SByte,Single, String,TimeSpan,UInt16,UInt32,UInt64
        switch (columnType)
        {
            case "System.Int32":
                sqlQuery += " int ";
                break;
            case "System.Int64":
                sqlQuery += " bigint ";
                break;
            case "System.Int16":
                sqlQuery += " tinyint ";
                break;
            case "System.Decimal":
                sqlQuery += " decimal ";
                break;
            case "System.DateTime":
                sqlQuery += " datetime ";
                break;
            case "System.String":
            default:
                sqlQuery += string.Format(" nvarchar({0}) ", table.Columns[i].MaxLength == -1 ? "max" : table.Columns[i].MaxLength.ToString());
                break;
        }

        if (table.Columns[i].AutoIncrement)
            sqlQuery  += " IDENTITY(" + table.Columns[i].AutoIncrementSeed.ToString() + "," + table.Columns[i].AutoIncrementStep.ToString() + ") ";

        if (!table.Columns[i].AllowDBNull)
            sqlQuery += " NOT NULL ";
        sqlQuery += ",";
    }

    return sqlQuery.Substring(0, sqlQuery.Length-1) + "\n)";
}

// Call the method
DataTable yourDataTableObj = new DataTable();
string sqlQuery = CreateTABLE(yourDataTableObj);

Step 2: Once you call the method you will get SQL query. Collect it and execute it in sqlserver manually or you can execute through ADO.NET
 
Share this answer
 
v3
Comments
NANDKUMAR GAIKWAD 19-Jan-16 6:47am    
thanx for suggestion.
and i'm new to here..but this is not excuse..
your answer is fabulous..
i send post my code and tell me what is wrong with that
[no name] 19-Jan-16 7:36am    
What is your actual problem: either generate database from Tally schema or binding data to drop downlist?
NANDKUMAR GAIKWAD 19-Jan-16 7:42am    
there is no problem with binding i'm trying i get schema from my below code

string source = "PORT=9000;DRIVER=Tally ODBC Driver;SERVER={(local)}";
OdbcConnection con = new OdbcConnection(source);
con.Open();
DataTable dt = new DataTable();
string[] restrictions = new string[1];
dt = con.GetSchema("Tables");
Database db = new Database();

DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "Table_Name";
DropDownList1.DataValueField = "Table_Name";
DropDownList1.DataBind();

i want how to generate table in sql server 2008 with this schema of table and there column name and there data type
but when i use your code i get that schema's datatable information
[no name] 19-Jan-16 7:55am    
If you are getting DataTable and its columns properly then follow my answer to generate SQL Query. Once you get SQL Query you need to execute in database.

Are you getting table schema properly from con variable?
NANDKUMAR GAIKWAD 19-Jan-16 8:01am    
yes i'm getting proper schema from i tried code..
but when i trying for single table or multiple table it not generate script

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900