错误是:
错误 3 应输入 class、delegate、enum、interface 或 struct C:\Documents and Settings\Administrator\桌面\20080217b\WindowsApplication2\WindowsApplication2\Form2.cs 141 17 WindowsApplication2
错误 4 应输入 class、delegate、enum、interface 或 struct C:\Documents and Settings\Administrator\桌面\20080217b\WindowsApplication2\WindowsApplication2\Form2.cs 146 17 WindowsApplication2
错误 5 应输入 class、delegate、enum、interface 或 struct C:\Documents and Settings\Administrator\桌面\20080217b\WindowsApplication2\WindowsApplication2\Form2.cs 151 17 WindowsApplication2
错误 6 应输入 class、delegate、enum、interface 或 struct C:\Documents and Settings\Administrator\桌面\20080217b\WindowsApplication2\WindowsApplication2\Form2.cs 156 17 WindowsApplication2
错误 7 应输入 class、delegate、enum、interface 或 struct C:\Documents and Settings\Administrator\桌面\20080217b\WindowsApplication2\WindowsApplication2\Form2.cs 164 17 WindowsApplication2
错误 8 应输入 class、delegate、enum、interface 或 struct C:\Documents and Settings\Administrator\桌面\20080217b\WindowsApplication2\WindowsApplication2\Form2.cs 177 17 WindowsApplication2
错误 9 应输入 class、delegate、enum、interface 或 struct C:\Documents and Settings\Administrator\桌面\20080217b\WindowsApplication2\WindowsApplication2\Form2.cs 182 17 WindowsApplication2
错误 10 应输入 class、delegate、enum、interface 或 struct C:\Documents and Settings\Administrator\桌面\20080217b\WindowsApplication2\WindowsApplication2\Form2.cs 184 40 WindowsApplication2
错误 11 应输入 class、delegate、enum、interface 或 struct C:\Documents and Settings\Administrator\桌面\20080217b\WindowsApplication2\WindowsApplication2\Form2.cs 187 38 WindowsApplication2
错误 12 应输入类型、命名空间定义或文件尾 C:\Documents and Settings\Administrator\桌面\20080217b\WindowsApplication2\WindowsApplication2\Form2.cs 193 5 WindowsApplication2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Web;
//using System.Web.UI;
//using System.Web.UI.WebControls;
//using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;namespace WindowsApplication2
{
    public partial class Form2 : Form
    {
       
        public Form2()
        {
            InitializeComponent();
        }        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {        }        private void button1_Click(object sender, EventArgs e)
        
        {
        
          
            //SqlConnection objconn = new SqlConnection("server=B2283AB125EA49D;database=master;uid=sa;pwd=771223");//未加入事务处理前写的连接语句             public class DbTranSql 
                  { //2
                  // file://将事务放到SQL Server中执行 
                   public void DoTran() 
                   { //1
                   // file://建立连接并打开 
                    SqlConnection myConn = GetConn();myConn.Open(); 
                    SqlCommand myComm = new SqlCommand(); 
                       //SqlTransaction myTran=new SqlTransaction(); 
                       SqlTransaction myTran; 
                       myTran = myConn.BeginTransaction(); 
                       
                    try 
                    { 
                     myComm.Connection = myConn; 
                         myComm.Transaction = myTran; 
                        myTran.Save("NoInsert"); 
                     myComm.CommandText = "DECLARE @TranName VARCHAR(20) "; 
                     myComm.CommandText += "SELECT @TranName = 'MyTransaction' "; 
                     myComm.CommandText += "BEGIN TRANSACTION @TranName "; 
                     myComm.CommandText += "USE master "; 
                     myComm.CommandText += "insert into 运动员报名表  (name,sid,class,grade,sex,project1,project2,jiti)  values  ('" + textBox3.Text.ToString() + "','" + textBox4.Text.ToString() + "','" + textBox2.Text.ToString() + "','" + comboBox5.Text.ToString() + "','" + comboBox1.Text.ToString() + "','" + comboBox2.Text.ToString() + "','" + comboBox3.Text.ToString() + "','" + comboBox4.Text.ToString() + "')"; 
                     myComm.CommandText += "COMMIT TRANSACTION MyTransaction "; 
                     myComm.ExecuteNonQuery(); 
                        myTran.Commit(); 
                    } 
                    catch(Exception err) 
                    { 
                      myTran.Rollback("NoInsert"); 
                          throw new ApplicationException("事务操作出错,系统信息:"+err.Message); 
                         
                    } 
                    finally 
                    { 
                     myConn.Close(); 
                    } 
                   } //1
                  // file://获取数据连接 
                   private SqlConnection GetConn() 
                   { 
                    string strSql="server=B2283AB125EA49D;database=master;uid=sa;pwd=771223"; 
                    SqlConnection myConn=new SqlConnection(strSql); 
                    return myConn; 
                   } 
                  } //2       
                  public class Test 
                  {//3 
                   public static void Main() 
                   { //4
                    DbTranSql tranTest=new DbTranSql(); 
                    tranTest.DoTran(); 
                    Console.WriteLine("事务处理已经成功完成。"); 
                    Console.ReadLine(); 
                   } //4
                  } 
    //原来正确的代码,为了用事务处理先屏蔽掉
       /*   if (comboBox2.Text == comboBox3.Text)
                MessageBox.Show("两个项目不能相同");
            else
            {
              if((comboBox1.Text=="女") && ((comboBox2.Text=="3000米") || (comboBox3.Text=="3000米")))
                MessageBox.Show("女生不能报3000米");
              else
                {                  
                  
                   string strSql = "update 竞赛分组表 set num = num+1 , contest_num  =  contest_num+'" + textBox4.Text.ToString() + " '   where (gro = '" + comboBox5.Text.ToString() + "'  and  sex = '" + comboBox1.Text.ToString() + "' and (project = '" + comboBox2.Text.ToString() + "' or project = '" + comboBox3.Text.ToString() + "' or project = '" + comboBox4.Text.ToString() + "')) ";
                  
                  SqlCommand objcmd = new SqlCommand(strSql, objconn);
                    objconn.Open();
                   objcmd.ExecuteNonQuery();
                    objconn.Close();   
                            string strSql2 = "insert into 运动员报名表  (name,sid,class,grade,sex,project1,project2,jiti)  values  ('" + textBox3.Text.ToString() + "','" + textBox4.Text.ToString() + "','" + textBox2.Text.ToString() + "','" + comboBox5.Text.ToString() + "','" + comboBox1.Text.ToString() + "','" + comboBox2.Text.ToString() + "','" + comboBox3.Text.ToString() + "','" + comboBox4.Text.ToString() + "')";
                SqlCommand objcmd2 = new SqlCommand(strSql2, objconn);
                objconn.Open();
                objcmd2.ExecuteNonQuery();
                objconn.Close();
                }
            }*/
        }
       
    }
}

解决方案 »

  1.   

    明明是语法错误,你在button_click方法里写这个干什么:
    public   class   DbTranSql   
      

  2.   

    你在事件里面写了一个类
    public   class   DbTranSql 就算不是类,在事件里面公共变量、私有变量、保护变量、都是不能申明的,你这代码不出错才怪!  
      

  3.   

    求助,能不能请soaringbird说明白些呢,将非常感谢啊
      

  4.   

    那把这个public   class   DbTranSql 去掉,只保留里面的事务处理怎么也是一样的错误呢,请指教哦
      

  5.   

    把DbTranSql 和Test类的代码放到Form2类的外边,在button_Click里像Test类的代码那样调用DbTranSql 
      

  6.   

    好乱的代码,不知道哪跟哪有关系!
    namespace   WindowsApplication2 

         public   partial   class   Form2   :   Form 
         { 
               public   Form2() 
               { 
                 InitializeComponent(); 
               }            private void comboBox1_SelectedIndexChanged(object sender,EventArgs e) 
               { 
               } 
               private   void   button1_Click(object   sender,   EventArgs   e) 
               { 
               }
         }
          public   class   DbTranSql   
         { //2 
        //   file://将事务放到SQL   Server中执行   
             public   void   DoTran()   
             {   //1 
                 //   file://建立连接并打开   
                SqlConnection   myConn   =   GetConn();myConn.Open();   
                SqlCommand   myComm   =   new   SqlCommand();   
                 //SqlTransaction   myTran=new   SqlTransaction();   
                 SqlTransaction   myTran;   
                 myTran   =   myConn.BeginTransaction();   
                 try   
                {   
                  myComm.Connection   =   myConn;   
                    myComm.Transaction   =   myTran;   
                    myTran.Save("NoInsert");   
                  myComm.CommandText =   "DECLARE   @TranName   VARCHAR(20)   ";   
                  myComm.CommandText +=   "SELECT   @TranName   =   'MyTransaction'   ";   
                  myComm.CommandText +=   "BEGIN   TRANSACTION   @TranName   ";   
                  myComm.CommandText +=   "USE   master   ";   
                  myComm.CommandText +=   "insert   into   运动员报名表 
                     (name,sid,class,grade,sex,project1,project2,jiti)     values     ('"   +    textBox3.Text.ToString()   +   "','"   +   textBox4.Text.ToString()   +   "','"   +   textBox2.Text.ToString()   +   "','"   +   comboBox5.Text.ToString()   +   "','"   +   comboBox1.Text.ToString()   +   "','"   +   comboBox2.Text.ToString()   +   "','"   +   comboBox3.Text.ToString()   +   "','"   +   comboBox4.Text.ToString()   +   "')";   
                  myComm.CommandText   +=   "COMMIT   TRANSACTION   MyTransaction   ";   
                  myComm.ExecuteNonQuery();   
                    myTran.Commit();   
                }   
                catch(Exception   err)   
                {   
                   myTran.Rollback("NoInsert");   
                    throw   new   ApplicationException("事务操作出错,系统信息:"+err.Message);   
                 }   
               finally   
               {   
                 myConn.Close();   
               }   
            }   //1 
            //   file://获取数据连接   
            private   SqlConnection   GetConn()   //没见里面有调用
            {   
            string   strSql="server=B2283AB125EA49D;database=master;uid=sa;pwd=771223";   
            SqlConnection   myConn=new   SqlConnection(strSql);   
            return   myConn;   
            }   
        }   //2       public   class   Test   
          {//3   
            public   static   void   Main()   
            {   //4 
              DbTranSql   tranTest=new   DbTranSql();   
              tranTest.DoTran();   
              Console.WriteLine("事务处理已经成功完成。");   
              Console.ReadLine();   
             }   //4 
          }   
    }
    后面的你不要了,我也不贴了,难贴。