日期:2014-05-18  浏览次数:20878 次

C#和数据库连接,sql语句中怎样把变量不要错认为数据库字段名
private int[] person; 
 public ZJBZ()
{
int[] Face = load_face(No); // 调用获取病例号=No的面色资料  
  }

  private int[] load_face(int person_No)
  {
  SqlConnection dataConnection = new SqlConnection();
  try
  {
  dataConnection.ConnectionString = "Integrated Security=true;" +
  "Initial catalog=ZYZD1230;" +
  "Data Source=ZHOUMIN\\SQLEXPRESS";
  dataConnection.Open();
  SqlCommand dataCommand = new SqlCommand();
  dataCommand.Connection = dataConnection;
  dataCommand.CommandText = "SELECT * FROM tFace WHERE tFace.No= person_No"; SqlDataReader datareader = dataCommand.ExecuteReader();
  datareader.Read();
  person = new int[4] ;
  for (int i = 0, j = 3; i <= j; i++)
  {
  person [i] = datareader.GetInt32(i + 1);
  Console.WriteLine(person[i]);
  }
  datareader.Close();
  }
  catch (Exception e)
  {
  Console.WriteLine("Error accessing the database: " + e.Message);
  }
  return person;
  }
在运行的时候,把蓝色代码中的person_No当作是字段名了,但是应该是方法调用时(红色部分)一个INT变量person_No,请问这个问题如何解决啊!
先谢谢各位大哥了。

------解决方案--------------------
Commandext = "Select * from tFace where tFace.No = " + person_No.ToString();