可以帮助我制作自动生成ID
本文关键字:自动生成 ID 帮助 | 更新日期: 2023-09-27 18:29:15
我有表单想要从主键自动生成该ID所以像这样的代码
private void setidphoto()
{
DataTable dt = con.FillTable("SELECT IDPhoto FROM Produksi ORDER BY IDPhoto DESC");
if (dt.Rows.Count == 0)
{
txtIdPhoto.Text = "PH001";
}
else {
string temp = dt.Rows[0][0].ToString();
int IDplus= int.Parse(temp.Substring(2)) + 1;
string NewID = String.Format("PH{0:000}", IDPlus);
txtIdPhoto.Text = NewID;
}
}
错误是con.FillTable
无法编译或运行它,你能帮我
试试这个
SqlConnection con = new SqlConnection("yourconnectionstringhere");
SqlCommand com = new SqlCommand("SELECT IDPhoto FROM Produksi ORDER BY IDPhoto DESC", con);
SqlDataAdapter adp = new SqlDataAdapter(com);
DataTable dt = new DataTable();
add.SelectCommand = com;
adp.Fill(dt);