Ald*_*rez 2 c# database sql-server stored-procedures
我正在尝试在我的数据库中插入值,每当我尝试添加它时,我的程序就会崩溃,给我一个错误,即ConnectionString属性尚未初始化,但我正确初始化它.这是我的代码:
private static string conStr = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\aldrin\documents\visual studio 2013\Projects\MidtermAssignment_Ramirez\MidtermAssignment_Ramirez\MasterFile.mdf;Integrated Security=True";
SqlConnection myCon = new SqlConnection();
private int studId, year, units;
private string fName, lName, mName, course, payment;
public void insertRecord()
{
myCon.Open();
SqlCommand insertInfo = new SqlCommand("spInsertStudentInformation", myCon);
insertInfo.CommandType = CommandType.StoredProcedure;
insertInfo.Parameters.Add("@studId", SqlDbType.Int).Value = studId;
insertInfo.Parameters.Add("@fName", SqlDbType.VarChar).Value = fName;
insertInfo.Parameters.Add("@lName", SqlDbType.VarChar).Value = lName;
insertInfo.Parameters.Add("@mName", SqlDbType.VarChar).Value = mName;
insertInfo.ExecuteNonQuery();
myCon.Close();
myCon.Open();
SqlCommand insertData = new SqlCommand("spInsertStudentData", myCon);
insertData.CommandType = CommandType.StoredProcedure;
insertData.Parameters.Add("@studId", SqlDbType.Int).Value = studId;
insertData.Parameters.Add("@course", SqlDbType.VarChar).Value = course;
insertData.Parameters.Add("@year", SqlDbType.Int).Value = year;
insertData.Parameters.Add("@units", SqlDbType.Int).Value = units;
insertData.Parameters.Add("@payment", SqlDbType.VarChar).Value = payment;
insertData.ExecuteNonQuery();
myCon.Close();
}
Run Code Online (Sandbox Code Playgroud)
这是我的按钮中的代码:
myData.StudId = Convert.ToInt32(txtStudId.Text);
myData.FName = txtFName.Text;
myData.LName = txtLName.Text;
myData.MName = txtMName.Text;
myData.Course = cboCourse.SelectedItem.ToString();
myData.Year = Convert.ToInt32(cboYear.SelectedItem.ToString());
myData.Units = Convert.ToInt32(txtUnits.Text);
myData.Payment = cboPayment.SelectedItem.ToString();
myData.insertRecord();
Run Code Online (Sandbox Code Playgroud)
这是我的存储过程:
CREATE PROCEDURE [dbo].spInsertStudentData
@studId int,
@course varchar(50),
@year int,
@units int,
@payment varchar(50)
AS
INSERT INTO StudentData VALUES (@studId, @course, @year, @units, @payment)
RETURN 0
CREATE PROCEDURE [dbo].spInsertStudentInformation
@studId int,
@fName varchar(50),
@lName varchar(50),
@mName varchar(50)
AS
INSERT INTO StudentInformation VALUES (@studId, @fName, @lName, @mName)
RETURN 0
Run Code Online (Sandbox Code Playgroud)
我最近在ASP.NET学习数据库,这就是我正在做的,但我不知道为什么这在C#中运行不正常.
错误消息很明确.在尝试打开连接之前,SqlConnection对象需要一个连接字符串.因此,在构建连接时,您需要传递连接字符串或在打开之前设置ConnectionString属性.
但我真的建议你开始使用本地连接变量而不是全局连接变量.并将此声明保留在using语句中
private static string conStr = @".....";
public void insertRecord()
{
using(SqlConnection myCon = new SqlConnection(conStr))
using(SqlCommand insertInfo = new SqlCommand("spInsertStudentInformation", myCon))
{
myCon.Open();
insertInfo.CommandType = CommandType.StoredProcedure;
....
insertInfo.ExecuteNonQuery();
// No need to close the connection here....
SqlCommand insertData = new SqlCommand("spInsertStudentData", myCon);
insertData.CommandType = CommandType.StoredProcedure;
....
insertData.ExecuteNonQuery();
}
}
Run Code Online (Sandbox Code Playgroud)
SqlConnection使用连接池基础结构,因此最好具有可由连接池回收的本地变量,而无需为程序保留重要资源
最后,using语句避免了危险的内存泄漏关闭和处理一次性对象,如连接和命令,如果异常
您的代码中还有另一点需要注意.您执行两个相关的命令.如果由于某种原因您无法插入学生数据,我确信您不想插入学生信息.这些场景要求打开一个SqlTransaction,使您的数据保持原子状态(这意味着如果任何操作失败,您不希望更改数据库中的任何内容)
using(SqlConnection myCon = new SqlConnection(conStr))
using(SqlCommand insertInfo = new SqlCommand("spInsertStudentInformation", myCon))
{
myCon.Open();
using(SqlTransaction ts = myCon.BeginTransaction())
{
insertInfo.CommandType = CommandType.StoredProcedure;
insertInfo.Transaction = ts;
....
insertData.CommandType = CommandType.StoredProcedure;
insertData.Transaction = ts;
....
insertData.ExecuteNonQuery();
ts.Commit();
}
}
Run Code Online (Sandbox Code Playgroud)
最终的Commit将保留数据库中的所有内容,而退出using块而不调用Commit,将自动回滚对表的每个更改
| 归档时间: |
|
| 查看次数: |
33760 次 |
| 最近记录: |