数据库是Access,表tb的结构是 nid 数字 默认值为0 主键 非自增,name 文本,department 文本;空表。
protected int selectID(string nid,string tablename)
{
int id;
string connectionstring = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
string connection = connectionstring + Server.MapPath("App_Data/Goverment.mdb");
OleDbConnection conn = new OleDbConnection(connection);
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
string strcmd = "select max("+nid+") from "+tablename;
cmd.CommandText = strcmd;
try
{
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count < 0)
{
id = 1;
}
else
{
id = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()) + 1;//错误提示:输入字符串的格式不正确
}
}
catch(Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
return id;
}
id = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()) + 1提示错误:输入字符串格式不正确这是什么原因?忘大家帮帮忙看看!
protected int selectID(string nid,string tablename)
{
int id;
string connectionstring = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
string connection = connectionstring + Server.MapPath("App_Data/Goverment.mdb");
OleDbConnection conn = new OleDbConnection(connection);
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
string strcmd = "select max("+nid+") from "+tablename;
cmd.CommandText = strcmd;
try
{
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count < 0)
{
id = 1;
}
else
{
id = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()) + 1;//错误提示:输入字符串的格式不正确
}
}
catch(Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
return id;
}
id = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()) + 1提示错误:输入字符串格式不正确这是什么原因?忘大家帮帮忙看看!
看看是什么值啊?
object o = cmd.ExecuteScalar();
if (Object.Equals(o, null))
{
id = 1;
}
else
{
id = (int)o + 1;
}
设个断点 看看ds里面的数据 我估计ds 里面有一行 ds.Tables[0].Rows[0][0].ToString()不能转换为数字 可能是null和空 绝对是