处女问题
本人第一问...............
使用access数据库,执行插入,想返回刚插入数据的ID,代码如下
sqlstr = "insert into class(name,parentID,leverN,path) values(@name,@parentID,@leverN,@path) select scope_identity() ";
OleDbParameter[] paras ={new OleDbParameter( "@name ",OleDbType.VarChar),
new OleDbParameter( "@parentID ",OleDbType.Integer),
new OleDbParameter( "@leverN ",OleDbType.Integer),
new OleDbParameter( "@path ",OleDbType.VarChar)};
paras[0].Value = newsclass.Name;
paras[1].Value = newsclass.ParentID;
paras[2].Value = newsclass.LeverN;
paras[3].Value = newsclass.Path;
int id = DataHelp.AccessHelper.ExeSqlWithParaReturnID(sqlstr, paras);//调用方法ExeSqlWithParaReturnID()看下面:
------------------------
public static int ExeSqlWithParaReturnID(string sqlstr, OleDbParameter[] parms)
{
int id = 0;
using (OleDbConnection conn = new OleDbConnection(myconn))
{
OleDbCommand cmd = conn.CreateCommand();
PrepareCmd(cmd, parms);
cmd.CommandText = sqlstr;
conn.Open();
try
{
id = Convert.ToInt32(cmd.ExecuteScalar());
}
catch (OleDbException e) { conn.Close(); throw new Exception(e.Message); }//抛出异常,说 SQL 语句的结束位置缺少分号 (;)。
&nbs