为什么我的插入没有在数据库中插入记录

时间:2012-11-09 16:20:07

标签: asp.net sql ado.net

我正在尝试使用以下方法将记录插入数据库。我在按钮单击事件中调用此方法但由于某些原因没有插入记录。

需要插入四个字段:rpttemplateid - 我从另一个数据库表获取该字段,所有其他字段只是静态值。

下面我做错了什么?

public void updatereporttemplate()
{
    string cnn = WebConfigurationManager.ConnectionStrings["Underwriting"].ConnectionString;
    SqlConnection cnn1 = new SqlConnection(cnn);
    cnn1.Open();
    string getrptdesc = "select max(rptdesc) + 1 from report_description where rptdesc < 999 and rptdesc is not null";
    SqlCommand cmd = new SqlCommand(getrptdesc, cnn1);
    SqlDataReader sdr = cmd.ExecuteReader();
    sdr.Read();

    int getcount = int.Parse(sdr[0].ToString());
    sdr.Close();
    string commandtext1 = "INSERT INTO report_template" + "(rpttemplateid,rpttemplatedescr,minsubs,minmebers) " +
          " Values( " + getcount  + "," + "  " + " ,  " +  0  + "," + 0  + ")";
    SqlCommand command1 = new SqlCommand
    {
        CommandText = commandtext1,
        Connection = cnn1

    };

2 个答案:

答案 0 :(得分:2)

您缺少command1.ExecuteNonQuery();

另外,您是否考虑在表中使用IDENTITY列而不是尝试手动设置计数?如果该页面同时被多个人击中该怎么办?

答案 1 :(得分:0)

您需要打开连接

cnn1.Open();

然后执行命令

command1.ExecuteNonQuery();
相关问题