string sql = "insert into golfclubs values('"+clubName+"','"+Area+"','"+in_date+"');select @@identity as 'maxid'";
string id = "";
try
{
SqlCommand dtc = new SqlCommand(sql,odbc.getConn());
SqlDataReader dtr = dtc.ExecuteReader();
id = dtr["maxid"].ToString();
dtr.Close();
}
catch(Exception exc)
{
throw exc;
}
------------------------------------------
提示错误:在没有任何数据时进行无效的读取尝试
请高手指点,哪错了。

解决方案 »

  1.   

    insert into 怎么用了.ExecuteReader(); 用ExceteNonQuery()
      

  2.   

    我的sql里还有一个select @@identity as maxid,用ExceteNonQuery()能读取到数据吗?
      

  3.   

    该sql在查询分析器中执行可以得到maxid的值,并非无数据
      

  4.   

    if(myReader.Read()) 

       // 使用数据 

      

  5.   

    if(!dtr.read())  throw new Exception("没有数据写入数据库。");
    id = dtr["maxid"].ToString();
      

  6.   

    if(!dtr.Read())  throw new Exception("没有数据写入数据库。");
    id = dtr["maxid"].ToString();