0 امتیاز

با سلام 

من با استفاده از SMO یک دیتابیس و یک جدول ایجاد کردم  و الان میخوام اط طریق Linq با این جدول کار کنم، چطور ممکنه این کار؟ 

و سوال بعدی هم  اینکه چطور این جدول رو در datagridview نشون بدم

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;
        }


        

    }

 

از یک دکمه برای اجرای کد ها استفاده میکنم

 private void btnCreateDatabase_Click(object sender, EventArgs e)
        {
            SQL.CreateDatabse("BetaDB2");
            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("BetaDB2", "userha", ColumnList, null);
            
            
            
        }

 

لطفا وارد شده یا عضو شوید تا بتوانید سوال بپرسید

سوال جدید

2,337 سوال

2,871 پاسخ

3,725 دیدگاه

3,920 کاربر

دسته بندی ها

...