C#.net关于插入数据的问题
出现错误:“在位置0处没有任何行”
string MyConn = "server=192.168.1.106;uid=sa;pwd=;database=Licai;Trusted_Connection=no";
SqlConnection conn = new SqlConnection(MyConn);
DataSet MyDataSet = new DataSet();
conn.Open();
System.Text.StringBuilder strSql = new System.Text.StringBuilder();
int mainID = Convert.ToInt32(MyDataSet.Tables[0].Rows[0]["Main_ID"]);//<-------就在这报的错
strSql.Append("insert into Pocket_book (");
if (rdoIncome.Checked)
{
strSql.Append(" Main_ID");
strSql.Append(" IO_Project");
strSql.Append(" ,IO_date");
strSql.Append(" ,Input_Money");
strSql.Append(" ,Project_Explain");
strSql.Append(" ,Delete_Mark");
strSql.Append(" )values( ");
strSql.Append(mainID + 1);
strSql.Append(" ,'" + cboIncome.Text + "'");
strSql.Append(" ,'" + dtpkDate.Value + "'");
strSql.Append(" ,'" + txtInMoney.Text + "'");
strSql.Append(" ,'" + txtExplain.Text + "'");
strSql.Append(" ,'0'");
strSql.Append(" )");
Main_ID在数据库中是主键
本人菜鸟,求各位帮忙看下,在线等,谢谢了!!!!!!
------解决方案--------------------
这样取mainID
C# code
string SQL = @"SELECT MAX [Main_ID] FROM [Pocket_book]";
SqlCommand cmd = new SqlCommand(SQL, conn);
int mainID = Convert.ToInt32(cmd.ExecuteScalar());