在Text文本中放SQL语句,在。NET怎么去调用他

解决方案 »

  1.   

    SqlConnect.open();
    SqlCommand.CommandText=TextBox.Text;
    SqlCommand.Execuse
      

  2.   

    1将每一个sql加到ArrayList的对象里
    2循环ArrayList的对象里的对象,一句一句执行sql语句
      

  3.   

    public static bool ExeTransactionS(System.Collections.ArrayList o)
    {
    string str_path=System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath);
    // OleDbConnection con = new OleDbConnection(System.Configuration.ConfigurationSettings.AppSettings[0]+str_path+System.Configuration.ConfigurationSettings.AppSettings[1]);
    OleDbConnection con = new OleDbConnection(System.Configuration.ConfigurationSettings.AppSettings[0]);
    con.Open();
    OleDbTransaction tran = con.BeginTransaction();
    OleDbCommand cmd = con.CreateCommand();
    cmd.CommandTimeout=5000;
    cmd.Transaction = tran;
    try
    {
    for(int i=0;i<o.Count;i++)
    {
    cmd.CommandText = o[i].ToString();
    cmd.ExecuteNonQuery();
    }
    tran.Commit();
    }
    catch(OleDbException e)
    {

    try
    {
    tran.Rollback();
    }
    catch (OleDbException ex)
    {
    if (tran.Connection != null)
    {
    System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('在尝试回滚事务的时候发生"
    +ex.Message+"错误!请联系管理员维护数据!')</script>");
    return false;
    }
    }
    System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('在尝试写入数据的时候发生"
    +e.Message+"的错误!数据安全!')</script>");
    return false;
    }
    finally
    {
    con.Close();
    }
    return true;
    }
      

  4.   

    调用:
    ArrayList a=new ArrayList();
    string sql="";//你的sql
    a.add(sql);
    sql="";//你的sql
    a.add(sql);
     if (ExeTransactionS(conn,a));//conn你的连接字符串
    {
      //成功处理
    }
    else
    {
    //失败处理
    }
      

  5.   

    string str = TextBox1.text;
    .......
    SqlCommand com = new SqlCommand(str,conn);
    .................
      

  6.   

    private void CreateDataBase ()
    {
    Process p = new Process(); 
    p.StartInfo.FileName = "cmd.exe"; 
    p.StartInfo.UseShellExecute = false; 
    p.StartInfo.RedirectStandardInput = true; 
    p.StartInfo.RedirectStandardOutput = false; 
    p.StartInfo.RedirectStandardError = true; 
    p.StartInfo.CreateNoWindow = true; 
    string Path = Application.StartupPath.ToString();
    string Parameter = "osql.exe -U " + uid + " -P " + pwd + " -S  "+ ServerName +" -i " + Path + @"\IPMS.sql";
    try 

    this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
    p.Start(); 
    p.StandardInput.WriteLine(Parameter); 
    p.StandardInput.WriteLine("exit"); 
    p.StandardInput.WriteLine("exit");
    p.WaitForExit(); 
    p.Close();

    catch(Exception e) 

    MessageBox.Show(e.Message);
    this.Close();

    }