使用SMO创建Column-SQL DB时,设置DataType失败

时间:2017-11-09 15:35:25

标签: c# sql smo

我正在尝试使用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:

 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中遇到异常 error message

异常消息告诉我在此函数::

中设置列的数据类型存在问题
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;
        }

1 个答案:

答案 0 :(得分:0)

我遇到了完全相同的问题,我通过使用构造函数创建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();