【发布时间】:2012-11-09 16:20:06
【问题描述】:
我正在尝试使用以下方法将记录插入数据库。我在按钮单击事件中调用此方法,但由于某些原因没有插入记录。
需要插入四个字段: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
};
【问题讨论】:
-
你在哪里执行 INSERT 查询字符串?
-
我在按钮单击事件中执行插入
-
不,但是您需要在 command1 对象上调用 execute() 对吗?否则 command1 将如何插入?
-
你在哪里执行 commandtext1
-
@vamsikirankolla,重要但与此无关,“永远不要相信编写内联参数的开发人员”。 Jon Skeet 使用 SQL/DB 参数。