在做参数化往数据库插入数据时出现的
问题居然是变量名
SQL的方式:@username
我用的是Oracle数据库它用的方式是::username
改为Oracle的方式就行了
public string InsertUser(Users user)
{
//string sql = "insert into Tuser (user_id,username,password,gender,email,mphone)" +
// " values (FN_SYS_GETGUID(),@username,@password,@gender,@email,@mphone)";
OracleConnection oracleConnection = DBHelper.GetOracleConnection;
string rowId = string.Empty;
try
{
oracleConnection.Open();
//OracleCommand cmd = new OracleCommand(sql, oracleConnection);
OracleCommand cmd = oracleConnection.CreateCommand();
cmd.CommandText = "insert into Tuser (user_id,username,password,gender,email,mphone)" +
"values (FN_SYS_GETGUID(),:username,:password,:gender,:email,:mphone)";
cmd.Parameters.AddWithValue("username", user.UserName);
cmd.Parameters.AddWithValue("password", user.PassWord);
cmd.Parameters.AddWithValue("gender", user.Gender);
cmd.Parameters.AddWithValue("email", user.Email);
cmd.Parameters.AddWithValue("mphone", user.Mphone);
OracleString oracleRowId;
cmd.ExecuteOracleNonQuery(out oracleRowId);
rowId = oracleRowId.Value;
}
catch (OracleException)
{ }
finally
{
oracleConnection.Close();
}
return rowId;
}