在当前上下文中不存在

在当前上下文中不存在

本文介绍了在当前上下文中不存在“变量"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一条错误消息,指出"sql"在当前上下文中不存在.有人可以告诉我解决方法吗?这是示例代码...

I am getting an error that says "The name ''sql'' does not exist in the current context. Can someone tell me a work around for this? here is the sample code...

if (btnUpdate.Text == "New")
           {
               string sql = "insert into MorningBriefings (department, date, equipment, staffing, specialprocedures, notes, clinics) values (@department, @date, @equipment, @staffing, @specialprocedures, @notes, @clinics)";
           }
           else
           {
               string sql = "update MorningBriefings set equipment=@equipment, staffing=@staffing, specialprocedures=@specialprocedures, notes=@notes, clinics=@clinics where department=@department and date=@date";
           }

           try
           {
               conn.Open();
               SqlCommand cmd = new SqlCommand(sql, conn);
            ....
           }

推荐答案

string sql;
if(btnUpdate.Text == "New")
{
   sql = "insert into MorningBriefings (department, date, equipment, staffing, specialprocedures, notes, clinics) values (@department, @date, @equipment, @staffing, @specialprocedures, @notes, @clinics)";
}
else
{
   sql = "update MorningBriefings set equipment=@equipment, staffing=@staffing, specialprocedures=@specialprocedures, notes=@notes, clinics=@clinics where department=@department and date=@date";
}

 try
 {
    conn.Open();
    SqlCommand cmd = new SqlCommand(sql, conn);
    ....
 }



这篇关于在当前上下文中不存在“变量"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 04:11