无法在此范围内声明名为“cmdDatabase”的局部变量,因为它会为“cmdDatabase”赋予不同的含义,

时间:2011-06-08 04:03:58

标签: c#

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {


            MySqlConnection conDatabase = new MySqlConnection("Data Source=localhost;" +
                                       "Persist Security Info=yes;" +
                                       "UserId=tee; PWD=t421039; database=ph3;");
            conDatabase.Open();
            MySqlCommand cmdDatabase = new MySqlCommand("DROP TABLE IF EXISTS `q_mem_sur`; create table q_mem_sur as SELECT Count(*) As rowa,  member.Ssurname,  Sum(Case When ((member.status = '1')) Then 1 Else 0 End) As Status11 From member Group By member.Ssurname Order By rowa Desc;", conDatabase);



     string[] arr = new string[8];       
    arr[0] = "UPDATE `member` SET `amphurecode`= SUBSTRING(member.own,3,4)";               
    arr[1] = "UPDATE `member` SET `provincecode`= SUBSTRING(member.own,3,2)";
    arr[2] = "DROP TABLE IF EXISTS `q_mem_tim`; create table q_mem_tim as SELECT member.idmember, member.own, member.provincecode, province.PROVINCE_NAME, member.amphurecode, amphur.AMPHUR_NAME, member.Sname, member.Ssurname, member.Hno, member.Moo, member.Sex, member.tambol, member.dateofbirth, member.migratedate, Year( Current_Date( ) ) -  Year( member.dateofbirth ) AS y,  DATEDIFF('2011-08-01',(migratedate)) AS d FROM member LEFT JOIN amphur ON ( member.amphurecode  = amphur.AMPHUR_CODE ) LEFT JOIN province ON member.provincecode  = province.PROVINCE_CODE";
    arr[3] = "DROP TABLE IF EXISTS `q_mem_sur`; create table q_mem_sur as SELECT Count(*) As rowa,  member.Ssurname,  Sum(Case When ((member.status = '1')) Then 1 Else 0 End) As Status11 From member Group By member.Ssurname Order By rowa Desc";
    arr[4] = "DROP TABLE IF EXISTS `q_mem_hno`;create table q_mem_hno as select member.Hno, member.Moo, member.tambol, COUNT( member.Hno ) AS cntHno, COUNT(DISTINCT member.Ssurname) as NoSur ";
    arr[5] = "DROP TABLE IF EXISTS `q_pro_ori`;create table q_pro_ori as Select member.provincecode,   count(*) As cnt  From member Group By member.provincecode Order By cnt Desc ";
    arr[6] = "DROP TABLE IF EXISTS `q_am_ori`;create table q_am_ori as Select member.amphurecode,  member.provincecode,  count(*) As cnt  From member Group By member.amphurecode, member.provincecode Order By cnt Desc ";
    arr[7] = "DROP TABLE IF EXISTS `Sur_Hno_`;create table Sur_Hno_ as SELECT count( *  ) , Ssurname, q_mem_tim.Hno, q_mem_tim.Moo, q_mem_tim.tambol FROM q_mem_tim";

    foreach (string s in arr)
    {
        Console.WriteLine(s);
        MySqlCommand cmdDatabase = new MySqlCommand((s), conDatabase);
        cmdDatabase.CommandTimeout = 500;
        cmdDatabase.ExecuteNonQuery();
    }


            conDatabase.Close();


        }
    }
}

4 个答案:

答案 0 :(得分:0)

U有2个名为cmdDatabase的变量声明,第5行为foreach second。错误的说法是错误的......

答案 1 :(得分:0)

MySqlCommand cmdDatabase = new MySqlCommand((s), conDatabase);

它隐藏了以下代码:

    MySqlCommand cmdDatabase = new MySqlCommand("DROP TABLE IF EXISTS `q_mem_sur`; create table q_mem_sur as SELECT Count(*) As rowa,  member.Ssurname,  Sum(Case When ((member.status = '1')) Then 1 Else 0 End) As Status11 From member Group By member.Ssurname Order By rowa Desc;", conDatabase);

答案 2 :(得分:0)

你;在循环中需要为命令变量使用不同的名称。

答案 3 :(得分:0)

不要在foreach内重新声明变量,变量名已在较大的声明空间中使用。最多,您可以使用的代码重新实例化变量,而无需行上的类型声明。

 cmdDatabase = new MySqlCommand(s, conDatabase);

但这不太可能是你想要做的。您应该更新CommandText属性和任何参数,并保持相同的实例。您应 继续并养成使用using语句的习惯,以便正确关闭和处理您的连接和命令对象。

using (MySqlConnection myConnection = ...)
using (MySqlCommand myCommand = ...)
{
     // database code here
}