首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c# 建库 建表的方法哪位高手能帮小弟我写一个类

2013-11-02 
c# 建库 建表的方法谁能帮我写一个类c# 建库 建表的方法谁能帮我写一个类[解决办法] private Boolean Crea

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

c# 建库 建表的方法谁能帮我写一个类
[解决办法]
 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;
        }  
[解决办法]
上面两个函数,一个创建表格,一个创建函数.
下面的创建数据连结
  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;
        }

热点排行