使用 SMO 创建列 - SQL 数据库的设置数据类型失败



我正在尝试使用 SMO 创建带有 C# WinForm 应用程序的数据库。 我创建了这个类来管理事物:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SqlServer.Management.Smo;
using System.Collections.Specialized;
using Microsoft.SqlServer.Management.Common;
namespace WindowsFormsApp3
{
    class SqlServerController
    {
        //ServerConnection connection = new ServerConnection("MSSQLSERVER", "sysAdmin", "");
        private Server m_server=null;
        public SqlServerController (string server)
        {
            m_server = new Server(server);
        }
        public void AttachDatabase (string database, StringCollection files,AttachOptions options)
        {
            m_server.AttachDatabase(database, files, options);
        }
        public void AddBackupDevice(string name)
        {
            BackupDevice device = new BackupDevice(m_server,name);
            m_server.BackupDevices.Add(device);
        }
        public string GetServerVersion(string serverName)
        {
            return m_server.PingSqlServerVersion(serverName).ToString();
        }
        public int CountActiveConnections(string database)
        {
            return m_server.GetActiveDBConnectionCount(database);
        }
        public void DeleteDatabase (string database)
        {
            m_server.KillDatabase(database);
        }
        public void DetachDatabse (string database, bool updatestatistics,bool removeFullTextIndex)
        {
            m_server.DetachDatabase(database, updatestatistics, removeFullTextIndex);
        }
        public void CreateDatabse (string database)
        {
            Database db = new Database(m_server, database);
            db.Create();
        }
        public void CreateTable(string database, string table, List<Column> ColumnList,List<Index> IndexList)
        {
            Database db = m_server.Databases[database];
            Table newTable = new Table(db, table);
            foreach (Column column in ColumnList)
                newTable.Columns.Add(column);
            if (IndexList !=null)
            {
                foreach (Index index in IndexList)
                    newTable.Indexes.Add(index);
            }
            newTable.Create();
        }
        public Column CreateColumn (string name, DataType type, string @default,bool isIdentity,bool nullable)
        {
            Column column = new Column();
            column.DataType = type;
            column.Default = @default;
            column.Identity = isIdentity;
            column.Nullable = nullable;
            return column;
        }
        public Index CreateIndex(string name, bool isClustered, IndexKeyType type,string[] columnNameList)
        {
            Index index = new Index();
            index.Name = name;
            index.IndexKeyType = type;
            index.IsClustered = isClustered;
            foreach (string columnName in columnNameList)
                index.IndexedColumns.Add(new IndexedColumn(index, columnName));
            return index;
        }

        //public object ShowTable(string database,string table)
        //{
        //    var allData = from 
        //}
    }
}

我还有一个简单的按钮来创建数据库,它运行代码 belove on click 事件:

 private void btnCreateDatabase_Click(object sender, EventArgs e)
        {
            SQL.CreateDatabse("BetaDB");
            List<Column> ColumnList = new List<Column>();
            ColumnList.Add(SQL.CreateColumn("id", DataType.Int,"" ,true, false));
            ColumnList.Add(SQL.CreateColumn("name", DataType.NVarChar(50), "", false, false));
            ColumnList.Add(SQL.CreateColumn("UID", DataType.NVarChar(200), "", false, false));
            ColumnList.Add(SQL.CreateColumn("Pass", DataType.NVarChar(50), "", false, false));
            SQL.CreateTable("BetaDB", "userha", ColumnList, null);

        }

创建表时,我在Visual Studio中遇到异常错误信息

异常消息告诉我,在此函数中设置列的数据类型时出现问题:

public Column CreateColumn (string name, DataType type, string @default,bool isIdentity,bool nullable)
        {
            Column column = new Column();
            column.DataType = type;    // exception message
            column.Default = @default;
            column.Identity = isIdentity;
            column.Nullable = nullable;
            return column;
        }

我遇到了完全相同的问题,我通过使用构造函数创建列来解决它:

var newTable = new Table(srv.Databases["MyDatabase"], "MyTable");
var c = new Column(newTable, "MyColumn");
c.DataType = new DataType(SqlDataType.NVarCharMax);
newTable.Columns.Add(c);
newTable.Create();

最新更新