我搜索了这个标题,但我找不到有用的东西。
看看我的代码:
string q = "exec searchUser {0}";
q = string.Format(q, id);
SqlCommand cm2 = new SqlCommand();
cm2.Connection = sqlCon;
cm2.CommandText = q;
SqlDataAdapter sqldadapter1 = new SqlDataAdapter(cm2);
DataTable dataTabel1 = new DataTable();
sqldadapter1.Fill(dataTabel1);
if (sqlCon.State != ConnectionState.Open)
{
sqlCon.Open();
}
cm2.ExecuteNonQuery();
if (sqlCon.State != ConnectionState.Closed)
{
sqlCon.Close();
}
string uname = dataTabel1.Rows[0][1].ToString();
string logName = dataTabel1.Rows[0][2].ToString();
//DateTime.Now;
int countTrue = listBox_checked.Items.Count;
int countFalse = listBox_LogicFault.Items.Count;
MessageBox.Show(countTrue+"");
MessageBox.Show(countFalse+"");
MessageBox.Show(DateTime.Now.ToString());
DateTime time = DateTime.Now;
//string format = "yyyy-MM-dd HH:mm:ss";
string query_insertReport = "exec insertReport {0},{1},{2},'{3}',{4}, {5},'{6}'";
query_insertReport = string.Format(query_insertReport, id, uname, logName, time, countFalse, countTrue, sendFilename.filename);
SqlCommand cmdInsertReport = new SqlCommand();
cmdInsertReport.Connection = sqlCon;
cmdInsertReport.CommandText = query_insertReport;
if(sqlCon.State != ConnectionState.Open)
{
sqlCon.Open();
}
int idd = Convert.ToInt32 (cmdInsertReport.ExecuteScalar());
MessageBox.Show(idd+"report ID value");
if (sqlCon.State != ConnectionState.Closed)
{
sqlCon.Close();
}
MessageBox.Show("Report Inserted");
我的代码的第二部分中有一个问题,我想在Reports表中插入报表,并使用ExecuteScalar()
获取每条记录的reportID,因为我想将其插入到其他表中。插入成功完成,但ExecuteScalar()
返回0值(错误值)。我在Reports表中有一些记录,数字从32开始。问题是什么?
这是我的存储过程:
ALTER PROCEDURE [dbo].[insertReport]
@userID int,
@userName nvarchar(50),
@logicName nvarchar(50),
@date datetime,
@faultNumber int,
@trueNumber int,
@fileName nvarchar(MAX)
AS
INSERT INTO Reports ([UserID], [UserName], [LogicName], [Date],[FaultNumber], [TrueNumber], [filename])
VALUES (@userID, @userName, @logicName, @date, @faultNumber, @trueNumber, @fileName)
答案 0 :(得分:1)
我遇到了什么问题。问题是SQl代码而不是C#和ADO.net.i需要添加此行代码。
ALTER procedure [dbo].[insertReport]
@userID int,
@userName nvarchar(50),
@logicName nvarchar(50),
@date datetime,
@faultNumber int,
@trueNumber int,
@fileName nvarchar(MAX)
as
INSERT into Reports ([UserID],[UserName],[LogicName],[Date],[FaultNumber],[TrueNumber],[filename]) values (@userID,@userName,@logicName,@date,@faultNumber,@trueNumber,@fileName)
select CAST (SCOPE_IDENTITY() As int)
我修改了我的程序后,现在ExecuteScalar()返回true值。 完整的程序代码:
{{1}}