用SQL Profiler檢查每個SQL語句的執行

解决方案 »

  1.   

    可以使用异常处理,可以停止执行后面的语句,并显示一条提示信息,然后返回主界面。或者使用Transatcion事务,支持backroll达到停止执行后面的语句
      

  2.   

    try
    {
       .............
       执行你的Sql语句;
    }
    catch(Exception ex)
    {
    MessageBox.Show("数据库操作错误:"+ex.Message,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
    ......................
    //返回主界面代码
    }
    finally
    {
    if(mCn==ConnectionState.Open)
    {
    mCn.Close();
    }
    }
      

  3.   

    最好写一个错误捕作类把所有的SQL错误ID号进行
    Catch(SqlException me)
    {}
      

  4.   

    大家说的方法都不行,我的代码如下(直贴出主要代码)
    try
    {
       mCommand=new SqlDataAdapter();
       sqlSelectCommand1=new SqlCommand();
       mCommand.SelectCommand=sqlSelectCommand1;
       sqlSelectCommand1.CommandText=textBox1.text;//
       sqlSelectCommand1.Connection=mConnection;
       mCommand.Fill(ds);//如果textBox1.text中的语句错误,执行到这一句就会出错.
       //我想如果textBox1.text中的语句错误,就停止执行后面的语句,并返回主界面.
    }
    catch(System.Exception E)
    {
       MessageBox.Show(E.ToString());
    }
      

  5.   

    你去查查msn
    里面有一些多少的ID号为数字的不同的错误结果的

    try
    {
    .....
    }
    catch(Exception h)
    {
    ........
    }
    finally
    {
    ......
    }
      

  6.   

    vs中可以单步调试存储过程,当然也就可以测试SQL语句
      

  7.   

    try
    {}
    catch (SqlExcepiton e)

    }这样呢?
      

  8.   

    把sql语句放到Sql Server的查询分析器里面去就可以判断什么地方出错了
      

  9.   

    下面的语句应该没有问题啊
    try
    {
       mCommand=new SqlDataAdapter();
       sqlSelectCommand1=new SqlCommand();
       mCommand.SelectCommand=sqlSelectCommand1;
       sqlSelectCommand1.CommandText=textBox1.text;//
       sqlSelectCommand1.Connection=mConnection;
       mCommand.Fill(ds);
    }
    catch(System.Exception E)
    {
       MessageBox.Show(E.Message);
    }finally
    {

    sqlSelectCommand1.Connection.Close();
    }
      

  10.   


    mCommand=new SqlDataAdapter();
      sqlSelectCommand1=new SqlCommand();
       mCommand.SelectCommand=sqlSelectCommand1;
       sqlSelectCommand1.CommandText=textBox1.text;//
       sqlSelectCommand1.Connection=mConnection;
    try
    {
       mCommand.Fill(ds);
    }catch(System.Excfeption e)
    {
     MessageBox.show(e.Tostring)
    }finally
    {
    sqlSelectCommand1.Connection.Close();
    }
      

  11.   

    private void Test()
    {
    string strConnectionString = "server=(local);user id=sa;password=xu;database=Northwind;Connect timeout=600";
    SqlDataAdapter daSql = new SqlDataAdapter();
    SqlConnection cnSql = new SqlConnection(strConnectionString);
    cnSql.Open();
    SqlTransaction tsSql=cnSql.BeginTransaction();
    try
    {
    SqlCommand cmdQuery = new SqlCommand();
    cmdQuery.Connection = cnSql;
    cmdQuery.Transaction = tsSql;
    cmdQuery.CommandText = textBox1.Text;
    cmdQuery.ExecuteNonQuery();
    }
    catch(System.Exception E)
    {
    MessageBox.Show(E.Message);
    } finally
    {
    tsSql.Rollback();
    cnSql.Close();
    }
    }