如题,我写了一段SQL语句declare @tag int;
if exists (select ID from SYSOBJECTS where ID=OBJECT_ID('Voltage')) 
set @tag=1;
ELSE set @tag=0;我在C#里面怎么才能引用到上面定义的@tag变量呢?

解决方案 »

  1.   

            public static void Main(string[] args)
            {
                string connectionString = "*****************************************";
                object returns;
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter p1 = new SqlParameter("@albumID",SqlDbType.Int);
                    p1.Direction = ParameterDirection.Input;
                    p1.Value=123;//这里设置输入参数
                    SqlParameter p2 = new SqlParameter("@returns",SqlDbType.Int);
                    p2.Direction=ParameterDirection.Output;
                    p2.Value = DBNull.Value;
                    cmd.Parameters.Add(p1);
                    cmd.Parameters.Add(p2);
                    cmd.CommandText="PRO_ExistsAlbumCover";
                    try
                    {
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        returns = cmd.Parameters["@returns"].Value;
                    }
                    catch(SqlException ex)
                    {
                        throw new Exception(ex.Message);
                    }
                    finally
                    {
                        cmd.Dispose();
                        conn.Close();
                    }                              
                }
                Console.WriteLine(returns);
                Console.ReadKey();
      

  2.   

    你是想说你从来都不知道有ADO.NET?唉。
      

  3.   

    sql的增删改查都有相关的参数,你可以试着引用嘛
      

  4.   

    你需要在C#中创建数据库的链接,并在“上下文里”执行那段SQL,并返回指定变量的值如果没在指定的上下文中,那就需要存储在内访问到的位置,eg: 函数,存储过程等。
      

  5.   

    declare @tag int;
    if exists (select ID from SYSOBJECTS where ID=OBJECT_ID('Voltage')) 
    set @tag=1;
    ELSE set @tag=0;
    select  @tag
    c# 执行