读取数据库表设计

发布于 2024-11-17 07:44:09 字数 4074 浏览 10 评论 0原文

我正在读取 dBase 数据库,然后将该表发送到 SQL Server 2008。

我现在的问题是如何正确获取列类型,现在我在所有字符类型上获取 varchar(255) 。

我知道我很久以前就做过一次,但不记得我是怎么做到的。

从 dBase 数据库导出到数据集:

 public DataSet getDataSetFromDBF(string path, string fileName)
 {
        OdbcConnection conn = new OdbcConnection(@"Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277");
        String query = @"SELECT * FROM " + @"C:\spcs\" + fileName.ToString() + ".dbf";
        try
        {
            OdbcCommand MyCommand = new OdbcCommand(query, conn);
            OdbcDataAdapter adapter = new OdbcDataAdapter(MyCommand); 
            DataSet ds = new DataSet();                
            adapter.FillSchema(ds, SchemaType.Mapped);
            conn.Open();
            adapter.Fill(ds);
            return ds;
        }
        catch (Exception ex)
        {
            exceptionLog(ex);
            return null
        }
}

然后导出到 MSSQL Server:

private bool createSqlQueryFromDbfFile(string fileName)
{
    StringBuilder sb = new StringBuilder();
    string query = "";
    string pathToDB = @"c:\spcs\";
    string tableName = fileName.Replace(".dbf", "");
    sb.Append("IF OBJECT_ID('" + tableName + "', 'U') IS NOT NULL " + Environment.NewLine);
    sb.Append("DROP TABLE " + tableName + ";" + Environment.NewLine);
    sb.Append("CREATE TABLE " + tableName + Environment.NewLine);
    sb.Append("(" + Environment.NewLine);
    webHallDB.DAL db = new webHallDB.DAL();        
    try
    {
        DataSet ds = db.getDataSetFromDBF(pathToDB, tableName);

        int kolumner = ds.Tables[0].Columns.Count;

        string[] kolumnNamn = new string[kolumner];
        bool primaryKeySet = false;
        for (int i = 0; i < kolumner; i++)
        {
            if (primaryKeySet == false)
            {
                sb.Append("id int PRIMARY KEY IDENTITY," + Environment.NewLine);
                primaryKeySet = true;
            }
            string kolumnTyp = getColumnType(ds.Tables[0].Columns[i]);
            // Sista kolumnen
            if (i == kolumner - 1)
            {

                kolumnNamn[i] = ds.Tables[0].Columns[i].ColumnName;
                sb.Append(ds.Tables[0].Columns[i].ColumnName + " " + kolumnTyp + ");" + Environment.NewLine);
            }
            else
            {
                kolumnNamn[i] = ds.Tables[0].Columns[i].ColumnName;
                sb.Append(ds.Tables[0].Columns[i].ColumnName + " " + kolumnTyp + "," + Environment.NewLine);
            }
        }            

        int rader = ds.Tables[0].Rows.Count;
        DateTime startTid = DateTime.Now;

        StringBuilder sbInsert = new StringBuilder();

        for (int row = 0; row < rader; row++)
        {
            sbInsert.Append(Environment.NewLine + "Insert Into " + tableName);

            sbInsert.Append(Environment.NewLine + " Values (");
            for (int col = 0; col < kolumner; col++)
            {
                if (col == (kolumner - 1))
                {
                    sbInsert.Append("'" + changeSpecialCharacters(ds.Tables[0].Rows[row][col].ToString()) + "');");
                }
                else
                {
                    sbInsert.Append("'" + changeSpecialCharacters(ds.Tables[0].Rows[row][col].ToString()) + "', ");
                }
            }
        }

        query = sb.ToString();
        string insertQuery = sbInsert.ToString();

        DateTime slutTid = DateTime.Now;

        db.executeQuery(query);
        if (db.executeQuery(insertQuery))
        {
            slutTid = DateTime.Now;
            logToGui("Tid för att köra query: " + (slutTid - startTid).TotalSeconds);
            return true;
        }
        else
        {
            slutTid = DateTime.Now;
            logToGui("Tid för att köra query: (failed)" + (slutTid - startTid).TotalSeconds);
            return false;
        }
    }
    catch (Exception ex)
    {
        logToGui("createSqlQueryFromDbfFile misslyckades!");
        logToGui(ex.Message);
        return false;
    }
}

I'm reading a dBase database and then sending that table to a SQL Server 2008.

My problem now is how am i able to get the column type correctly, right now i'm getting varchar(255) on all character types.

I know i did this once long time ago but cant remember how i did it.

Export from dBase database to a dataset:

 public DataSet getDataSetFromDBF(string path, string fileName)
 {
        OdbcConnection conn = new OdbcConnection(@"Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277");
        String query = @"SELECT * FROM " + @"C:\spcs\" + fileName.ToString() + ".dbf";
        try
        {
            OdbcCommand MyCommand = new OdbcCommand(query, conn);
            OdbcDataAdapter adapter = new OdbcDataAdapter(MyCommand); 
            DataSet ds = new DataSet();                
            adapter.FillSchema(ds, SchemaType.Mapped);
            conn.Open();
            adapter.Fill(ds);
            return ds;
        }
        catch (Exception ex)
        {
            exceptionLog(ex);
            return null
        }
}

And then to MSSQL Server:

private bool createSqlQueryFromDbfFile(string fileName)
{
    StringBuilder sb = new StringBuilder();
    string query = "";
    string pathToDB = @"c:\spcs\";
    string tableName = fileName.Replace(".dbf", "");
    sb.Append("IF OBJECT_ID('" + tableName + "', 'U') IS NOT NULL " + Environment.NewLine);
    sb.Append("DROP TABLE " + tableName + ";" + Environment.NewLine);
    sb.Append("CREATE TABLE " + tableName + Environment.NewLine);
    sb.Append("(" + Environment.NewLine);
    webHallDB.DAL db = new webHallDB.DAL();        
    try
    {
        DataSet ds = db.getDataSetFromDBF(pathToDB, tableName);

        int kolumner = ds.Tables[0].Columns.Count;

        string[] kolumnNamn = new string[kolumner];
        bool primaryKeySet = false;
        for (int i = 0; i < kolumner; i++)
        {
            if (primaryKeySet == false)
            {
                sb.Append("id int PRIMARY KEY IDENTITY," + Environment.NewLine);
                primaryKeySet = true;
            }
            string kolumnTyp = getColumnType(ds.Tables[0].Columns[i]);
            // Sista kolumnen
            if (i == kolumner - 1)
            {

                kolumnNamn[i] = ds.Tables[0].Columns[i].ColumnName;
                sb.Append(ds.Tables[0].Columns[i].ColumnName + " " + kolumnTyp + ");" + Environment.NewLine);
            }
            else
            {
                kolumnNamn[i] = ds.Tables[0].Columns[i].ColumnName;
                sb.Append(ds.Tables[0].Columns[i].ColumnName + " " + kolumnTyp + "," + Environment.NewLine);
            }
        }            

        int rader = ds.Tables[0].Rows.Count;
        DateTime startTid = DateTime.Now;

        StringBuilder sbInsert = new StringBuilder();

        for (int row = 0; row < rader; row++)
        {
            sbInsert.Append(Environment.NewLine + "Insert Into " + tableName);

            sbInsert.Append(Environment.NewLine + " Values (");
            for (int col = 0; col < kolumner; col++)
            {
                if (col == (kolumner - 1))
                {
                    sbInsert.Append("'" + changeSpecialCharacters(ds.Tables[0].Rows[row][col].ToString()) + "');");
                }
                else
                {
                    sbInsert.Append("'" + changeSpecialCharacters(ds.Tables[0].Rows[row][col].ToString()) + "', ");
                }
            }
        }

        query = sb.ToString();
        string insertQuery = sbInsert.ToString();

        DateTime slutTid = DateTime.Now;

        db.executeQuery(query);
        if (db.executeQuery(insertQuery))
        {
            slutTid = DateTime.Now;
            logToGui("Tid för att köra query: " + (slutTid - startTid).TotalSeconds);
            return true;
        }
        else
        {
            slutTid = DateTime.Now;
            logToGui("Tid för att köra query: (failed)" + (slutTid - startTid).TotalSeconds);
            return false;
        }
    }
    catch (Exception ex)
    {
        logToGui("createSqlQueryFromDbfFile misslyckades!");
        logToGui(ex.Message);
        return false;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡紫姑娘! 2024-11-24 07:44:09

我设法弄清楚:

adapter.FillSchema(ds, SchemaType.Mapped); 

然后我能够使用 DataColumn.MaxLength。

I managed to figure it out:

adapter.FillSchema(ds, SchemaType.Mapped); 

Then i was able to use DataColumn.MaxLength.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文