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

SqlCommand与OleDbCommand的区别
OleDB可以正常运行:  
  private const string SELECTUSERNAMESTRING = "Select * from Users where username=?";
  private OleDbCommand cmd = null;
  private OleDbConnection ole_con;

  protected void Page_Load(object sender, EventArgs e)
  {

  }
  protected void Button1_Click(object sender, EventArgs e)
  {
  DataSet data = GetUsersData("test");
  string a;
  a = data.Tables["Users"].Rows[0]["EMPID"].ToString();
  return;
  }

  private void BuildCommand(CommandType ctype, string storedProcName, IDataParameter[] parms)
  {
  cmd.CommandType = ctype;
  cmd.CommandText = storedProcName;
  cmd.Parameters.Clear();
  //cmd.CommandText = "Select * from Users where username='test'";
  //return;
  if (parms != null)
  {
  foreach (IDataParameter parm in parms)
  {
  cmd.Parameters.Add(parm);
  }
  }

  }
  public void Open()
  {
  string s;
  s = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;password= ;Initial Catalog=zhuzhoudbbus;Data Source=192.168.0.17";
  ole_con = new OleDbConnection(s);
  cmd = ole_con.CreateCommand();
  }

  public DataSet GetUsersData(string username)
  {
  IDataParameter[] parms =
  new IDataParameter[]{new OleDbParameter("USERNAME",OleDbType.VarChar,20,"USERNAME")
  };
  parms[0].Value = username;
  DataSet data = new DataSet();
  RunProcedure(CommandType.Text, SELECTUSERNAMESTRING, parms, data, "Users");
  return data;
  }
 
  public void RunProcedure(CommandType ctype, string storedProcName, IDataParameter[] parameters, DataSet dataSet, string tableName)
  {
  Open();

  BuildCommand(ctype, storedProcName, parameters);
  OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);

  adapter.Fill(dataSet, tableName);
  }
  protected void Button1_Click1(object sender, EventArgs e)
  {
  DataSet data = GetUsersData("test");
  string a;
  a = data.Tables["Users"].Rows[0]["UserID"].ToString();
  return;
  }

SQL却不行:
  private const string SELECTUSERNAMESTRING = "Select * from Users where username=?";
  private SqlCommand cmd = null;
  private SqlConnection ole_con;
   
  protected void Page_Load(object sender, EventArgs e)
  {

  }
  protected void Button1_Click(object sender, EventArgs e)
  {
  DataSet data = GetUsersData("test");
  string a;
  a = data.Tables["Users"].Rows[0]["EMPID"].ToString();
  return;
  }

  private void BuildCommand(CommandType ctype, string storedProcName, IDataParameter[] parms)
  {
  cmd.CommandType = ctype;
  cmd.CommandText = storedProcName;
  cmd.Parameters.Clear();
  //cmd.CommandText =