c# 建库 建表的方法谁能帮我写一个类

北京大白兔 2013-11-01 02:40:40

c# 建库 建表的方法谁能帮我写一个类
...全文
116 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
_小黑_ 2013-11-01
  • 打赏
  • 举报
回复
baidu PowerDesigner
capricciosoft 2013-11-01
  • 打赏
  • 举报
回复
Node Config等是我自定义的用来存储树形配置结构的东西,你不必在意这些,替换成相关的参数就可以了。因此,这几个函数你还要修改修改才可以用。
capricciosoft 2013-11-01
  • 打赏
  • 举报
回复
上面两个函数,一个创建表格,一个创建函数. 下面的创建数据连结 public SqlConnection Connection(Config config,String DataBaseName="") { Node dataConnectionNode=config.Root.SubNode("dataConnection"); String Service = dataConnectionNode.Property("dataService").Value; String InstanceName = dataConnectionNode.Property("instanceName").Value; String UserName = dataConnectionNode.Property("username").Value; String Password = dataConnectionNode.Property("password").Value; Boolean IntegratedSecurity = dataConnectionNode.Property("integratedSecurity").Value=="true"; String connectionString; connectionString = "Data Source=" + Service + "\\" + InstanceName + ";" + "Integrated Security=" + (IntegratedSecurity ? "true" : "false"); if (IntegratedSecurity) { connectionString = connectionString + ";uid=" + UserName + ";" + "pwd=" + Password; } if (DataBaseName != "") { connectionString = connectionString + ";DATABASE=" + DataBaseName; } try { m_Connection = new SqlConnection(connectionString); m_Connection.Open(); } catch { return null; } return m_Connection; }
capricciosoft 2013-11-01
  • 打赏
  • 举报
回复
private Boolean CreateDataBase(String name, SqlConnection sqlConnection) { String sqlCommandText = "create database " + name; SqlCommand sqlCommand = new SqlCommand(sqlCommandText, sqlConnection); try { sqlCommand.ExecuteScalar(); } catch { return false; } return true; } private Boolean CreateTable(String dataBaseName, String tableName, Node NodeConfig, SqlConnection sqlConnection) { String sqlCommandText = "use " + dataBaseName; SqlCommand sqlCommand = new SqlCommand(sqlCommandText, sqlConnection); sqlCommand.ExecuteScalar(); sqlCommandText = "CREATE TABLE " + tableName; int n = NodeConfig.Nodes.Count; String PKs = ""; for (int i = 0; i < n; i++) { Node node = NodeConfig.Nodes[i]; String name = node.Property("name").Value; String dateType = node.Property("dateType").Value; String prec = node.Property("prec").Value; String scale = node.Property("scale").Value; String defalut = node.Property("defaultValue").Value; Boolean IsNullable = node.Property("isNullable").Value == "true"; Boolean isPK = node.Property("isPK").Value == "true"; Boolean isIdentity = node.Property("isIdentity").Value == "true"; String identityStart = node.Property("identityStart").Value; String identityStep = node.Property("identityStep").Value; if (prec == "" || prec == "0") { prec = "128"; } if (scale == "") { scale = "0"; } if (i == 0) { sqlCommandText = sqlCommandText + "("; } sqlCommandText = sqlCommandText + name + " " + dateType; if (dateType == "decimal" || dateType == "numeric") { sqlCommandText = sqlCommandText + "(" + prec + "," + scale + ")"; } else if (dateType == "smallint" || dateType == "tinyint" || dateType == "char" || dateType == "varchar") { sqlCommandText = sqlCommandText + "(" + prec + ")"; } if (!IsNullable) { sqlCommandText = sqlCommandText + " NOT NULL"; } if ((!isIdentity) || dateType == "char" || dateType == "varchar" || dateType == "date" || dateType == "datetime") { if (defalut != "") { sqlCommandText = sqlCommandText + " DEFAULT '" + defalut + "'"; } } if (isPK) { if (PKs == "") { PKs = PKs + name; } else { PKs = PKs + "," + name; } } if (isIdentity && (dateType == "int" || dateType == "integer" || dateType == "smallint" || dateType == "tinyint" || dateType == "decimal" || dateType == "numeric")) { if (identityStart == "") { identityStart = "1"; } if (identityStep == "" || identityStep == "0") { identityStep = "1"; } sqlCommandText = sqlCommandText + " IDENTITY(" + identityStart + "," + identityStep + ")"; } if (i == n - 1) { if (PKs != "") { sqlCommandText = sqlCommandText + ",PRIMARY KEY(" + PKs + ")"; } sqlCommandText = sqlCommandText + ")"; } else { sqlCommandText = sqlCommandText + ","; } } sqlCommand = new SqlCommand(sqlCommandText, sqlConnection); try { sqlCommand.ExecuteScalar(); } catch { return false; } return true; }
threenewbee 2013-11-01
  • 打赏
  • 举报
回复
一样,调用sql 比如 create database xxx 以及 create table tablename (字段类型 字段名, ... )
卧_槽 2013-11-01
  • 打赏
  • 举报
回复
学两句sql语句难道就会累死?

110,525

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧