关闭

Excel--->SQL---->C#

标签: c#layoutstringdatabaseobjectcommand
529人阅读 评论(0) 收藏 举报

private void LinkButton1_Click(object sender, System.EventArgs e)
  {//获取数据库表
   SqlConnection m_Scon=new SqlConnection();
   try
   {  
    m_Scon.ConnectionString = "user id="+this.txtUserName.Text+
     ";password="+this.txtPassword.Text+
     ";data source="+this.txtServerName.Text;
    SqlCommand m_Scmd = new SqlCommand("sp_databases",m_Scon);
    m_Scmd.CommandType = CommandType.StoredProcedure;
    m_Scon.Open();
    SqlDataReader m_Sdr = m_Scmd.ExecuteReader();
    this.DropDownList3.Items.Clear();
    while(m_Sdr.Read())
    {
     ListItem m_LI = new ListItem();
     m_LI.Text = m_Sdr["DATABASE_NAME"].ToString();
     m_LI.Value = m_Sdr["DATABASE_NAME"].ToString();
     this.DropDownList3.Items.Add(m_LI);
    }
    m_Sdr.Close();
    m_Scon.Close();
   }
   catch
   {
    if(m_Scon.State.ToString().ToUpper()=="OPEN")
    {
     m_Scon.Close();
    }
   }
  }

private void LinkButton2_Click(object sender, System.EventArgs e)
  {//建立数据表
   string strConnection="uid="+this.txtUserName.Text+
     ";pwd="+this.txtPassword.Text+
     ";database="+this.DropDownList3.SelectedItem+
     ";server="+this.txtServerName.Text;
   SqlConnection Connection=null;
   Connection=new SqlConnection(strConnection);
   Connection.Open();
   string strCommand=this.txtCode.Text.Trim();
   SqlCommand Command=new SqlCommand(strCommand,Connection);
   Command.ExecuteNonQuery();
   this.LinkButton2.Text="数据表建立成功";
   Connection.Close();
  }
 

private void LinkButton6_Click(object sender, System.EventArgs e)
  {
   SqlConnection m_Scon=new SqlConnection();
   this.txtCode.Text=string.Empty;
   if(this.DropDownList1.SelectedIndex!=-1)
   {
    m_Scon.ConnectionString = "user id="+this.txtUserName.Text+
     ";password="+this.txtPassword.Text+
     ";initial catalog="+this.DropDownList3.SelectedItem.Text+
     ";data source="+this.txtServerName.Text;
    SqlCommand m_Scmd = new SqlCommand("sp_columns",m_Scon);
    m_Scmd.CommandType = CommandType.StoredProcedure;
    SqlParameter myParm = m_Scmd.Parameters.Add("@table_name",SqlDbType.VarChar,100);
    myParm.Value = this.DropDownList1.SelectedItem.Value;
    m_Scon.Open();
    SqlDataReader m_Sdr = m_Scmd.ExecuteReader();
    string TableName = this.DropDownList1.SelectedItem.Value;
    string m_LayOut = "";
  
    m_LayOut = m_LayOut + "using System;/r/n";
    m_LayOut = m_LayOut + "using System.Data;/r/n" ;
    m_LayOut = m_LayOut + "using System.Data.SqlClient;/r/n";
    m_LayOut = m_LayOut + "/r/n";
    m_LayOut = m_LayOut + "namespace 上海师范大学王超同学/r/n";
    m_LayOut = m_LayOut + "{/r/n";
  
    m_LayOut = m_LayOut + BL(4) + "///<comment>/r/n";
    m_LayOut = m_LayOut + BL(4) + "////r/n";
    m_LayOut = m_LayOut + BL(4) + "///</comment>/r/n";
  
    string ColName = ""; //列名
    string ColType = ""; //列的数据类型
    string ColDefine = ""; //列定义
    string ColProperty = ""; //列属性
    while(m_Sdr.Read())
    {
     ColName = m_Sdr["COLUMN_NAME"].ToString();
     ColType = GT(m_Sdr["TYPE_NAME"].ToString());
  
     ColDefine = ColDefine + BL(8) + "private " + ColType + " m_" + ColName + ";/r/n";
  
     ColProperty = ColProperty + BL(8) + "/r/n";
     ColProperty = ColProperty + BL(8) + "public " + ColType + " " + ColName + "/r/n";
     ColProperty = ColProperty + BL(8) + "{/r/n";
     ColProperty = ColProperty + BL(12) + "get/r/n";
     ColProperty = ColProperty + BL(12) + "{/r/n";
     ColProperty = ColProperty + BL(16) + "return m_" + ColName + " ;/r/n";
     ColProperty = ColProperty + BL(12) + "}/r/n";
     ColProperty = ColProperty + BL(12) + "set/r/n";
     ColProperty = ColProperty + BL(12) + "{/r/n";
     ColProperty = ColProperty + BL(16) + "m_" + ColName + " = value ;/r/n";
     ColProperty = ColProperty + BL(12) + "}/r/n";
     ColProperty = ColProperty + BL(8) + "}/r/n";
  
    }
    m_Sdr.Close();
    m_Scon.Close();
  
    m_LayOut = m_LayOut + BL(4) + "public class " + TableName + "/r/n";
    m_LayOut = m_LayOut + BL(4) + "{/r/n";
  
    m_LayOut = m_LayOut + BL(4) + "/r/n";
    m_LayOut = m_LayOut + ColDefine; //定义
  
    m_LayOut = m_LayOut + BL(8) + "///<summary>/r/n";
    m_LayOut = m_LayOut + BL(8) + "///构造函数/r/n";
    m_LayOut = m_LayOut + BL(8) + "///<summary>/r/n";
    m_LayOut = m_LayOut + BL(8) + "public " + TableName + "()/r/n"; //构造函数
    m_LayOut = m_LayOut + BL(8) + "{/r/n";
    m_LayOut = m_LayOut + BL(8) + "}/r/n/r/n";
  
    m_LayOut = m_LayOut + BL(8) + "///<summary>/r/n";
    m_LayOut = m_LayOut + BL(8) + "///析构函数/r/n";
    m_LayOut = m_LayOut + BL(8) + "///<summary>/r/n";
    m_LayOut = m_LayOut + BL(8) + "~" + TableName + "()/r/n"; //析构函数
    m_LayOut = m_LayOut + BL(8) + "{/r/n";
    m_LayOut = m_LayOut + BL(8) + "}/r/n";
  
    m_LayOut = m_LayOut + ColProperty; //实体属性
  
    m_LayOut = m_LayOut + BL(4) + "}/r/n";
  
    m_LayOut = m_LayOut + "}/r/n";
  
    this.txtCode.Text = m_LayOut;
   }

  }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:35926次
    • 积分:660
    • 等级:
    • 排名:千里之外
    • 原创:29篇
    • 转载:1篇
    • 译文:0篇
    • 评论:8条
    最新评论