我有一个示例winform应用程序,它使用数据网格视图来显示将使用按钮创建的SQL数据库的一些数据。 "创建数据库" buttun使用SMO在其中创建数据库和表。
所以现在我有一些问题:
第一:这种类型的数据库可以使用Linq to SQL吗?如果是,请指导我这样做!
第二:如果无法对此数据库使用linq
,那么如何在datagridview
中显示表格?
我正在使用类belove来创建数据库和表:
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);
}