【发布时间】:2014-05-03 02:10:23
【问题描述】:
我在名为 AddNewStudent 的数据库中有一个简单的存储过程:
CREATE PROCEDURE dbo.AddNewStudent(
@fName nvarchar(20),
@sName nvarchar(20),
@lName nvarchar(20),
@faculty nvarchar(10),
@specialty nvarchar(50),
@OKS smallint,
@StudentStat smallint,
@fak nvarchar(50),
@Course smallint,
@Porok nvarchar(5),
@Group int
)
AS
INSERT INTO [Students] (FirstName, SecondName, LastName, Faculty,
Specialty, OKS, StudentStatus, FakNumber, Course, Potok, [[Group]]])
VALUES (@fName , @sName, @lName, @faculty, @specialty, @OKS,
@StudentStat, @fak, @Course, @Porok, @Group)
RETURN 2;
当我通过 DatabaseExplorer (VS2013) 测试该过程时,一切正常,记录被插入到表中。但是当我在 c# 中调用该过程时,什么也没有发生。下面是我用来调用该过程的方法的代码:
public static bool InsertStudent (Student student)
{
StudentDataClassesDataContext dc = new StudentDataClassesDataContext();
try
{
int returnValue = dc.AddNewStudent(student.FirstName, student.SecondName, student.LastName, student.Faculty, student.Specialty, student.OKS, student.StudentStatus,
student.FakNumber, student.Course, student.Potok, student._Group_);
dc.SubmitChanges();
MessageBox.Show("Return Value : " + returnValue, "Info", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (Exception e)
{
MessageBox.Show("Exception : " + e.Message, "Info", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
return true;
}
返回值为 2 表示程序正常工作,但为什么表中没有插入记录?我读到使用 dc.SubmitChanges() 而不是 Commit。
【问题讨论】:
-
你看到带有
Return Value: 2的消息框吗? -
您使用的是什么连接字符串?
-
你在存储过程结束时提交了吗?
-
从 SSME 配置您的数据库,然后通过您的服务器资源管理器连接它,它会工作。我在 .net 中创建基于窗口的项目时遇到了同样的问题,所以这就是我摆脱它的方式..试试吧
-
Hamlet Hakobyan :是的,我收到 2 作为返回值,我在消息框中看到它 marc_s :我不使用连接字符串,因为我使用 DataContext 建立连接 Ramie :我没有提交,但是当从数据库调用该过程时,我也没有提交,但插入了记录。 dc.SubmitChanges() 也不做提交吗? SKT:你能告诉我如何从 SSME 配置我的数据库吗?
标签: c# sql .net sql-server stored-procedures