可以帮我制作自动生成ID吗?

时间:2016-01-07 10:35:27

标签: c# sql-server visual-studio primary-key

我的表单想要从主键自动生成该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无法编译或运行它,你能帮我吗

1 个答案:

答案 0 :(得分:0)

试试这个

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);