Ahm*_*Gha 3 c# sql-server class
我在SQL Server数据库中插入数据时遇到问题.我正在使用一个名为book的类,在我的表单设计中,当我想插入数据时,我收到错误.
class Book
{
private string _bookname;
private int _bookcode;
private int _numberofpages;
private string _author;
private string _publicationname;
private string _genre;
private int _publishyear;
private string _language;
SqlConnection _sqlcon = new SqlConnection();
public Book(SqlConnection sqlcon)
{
_sqlcon = sqlcon;
}
public string bookname
{
get { return _bookname; }
set { _bookname = value; }
}
public int bookcode
{
get { return _bookcode; }
set { _bookcode = value; }
}
public int numberofpages
{
get { return _numberofpages; }
set { _numberofpages = value; }
}
public string author
{
get { return _author; }
set { _author = value; }
}
public string publicationname
{
get { return _publicationname; }
set { _publicationname = value; }
}
public string genre
{
get { return _genre; }
set { _genre = value; }
}
public int publishyear
{
get { return _publishyear; }
set { _publishyear = value; }
}
public string language
{
get { return _language; }
set { _language = value; }
}
public void SaveBook()
{
SqlConnection _sqlcon = new SqlConnection();
SqlCommand com = new SqlCommand();
com.CommandType = CommandType.StoredProcedure;
com.Connection = _sqlcon;
com.CommandText = "PROC_BOOK1";
com.Parameters.Add("@nameofbook", SqlDbType.VarChar).Value = bookname;
com.Parameters.Add("@codeofbook", SqlDbType.Int).Value = bookcode;
com.Parameters.Add("@npages", SqlDbType.Int).Value = numberofpages;
com.Parameters.Add("@bookauthor", SqlDbType.VarChar).Value = author;
com.Parameters.Add("@publicationname", SqlDbType.VarChar).Value = publicationname;
com.Parameters.Add("@bookgenre", SqlDbType.VarChar).Value = genre;
com.Parameters.Add("@pyear", SqlDbType.Int).Value = publishyear;
com.Parameters.Add("@booklanguage", SqlDbType.VarChar).Value = language;
com.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud)
这是我的插入按钮:
public partial class Form1 : Form
{
private SqlConnection _sqlcon = new SqlConnection();
private Book _book1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnsabt_Click(object sender, EventArgs e)
{
_book1 = new Book(_sqlcon);
_book1.bookname = txtbookname.Text;
_book1.bookcode = Convert.ToInt32(txtbookcode.Text);
_book1.numberofpages = Convert.ToInt32(txtnumberofpages.Text);
_book1.author = txtauthor.Text;
_book1.publicationname = txtpublication.Text;
_book1.genre = txtgenre.Text;
_book1.publishyear = Convert.ToInt32(txtpublishyear.Text);
_book1.language = txtlanguage.Text;
_book1.SaveBook();
MessageBox.Show("saved succesfully");
}
Run Code Online (Sandbox Code Playgroud)
以下是错误快照:
您的问题与您设计此Book模型的方式有关.
您强制用户使用接收SqlConnection的构造函数然后(这可能只是一个错误)您在SaveBook方法中创建SqlConnection的新实例(没有连接字符串).
您可能希望使用构造函数中传递的SqlConnection,并且您希望此类的客户端使用适当的connectionstring设置对其进行初始化并打开它.
因此,如果您真的希望您的客户端提供连接,那么使用connectionstring初始化它并在click方法中初始化Book实例之前打开它.当然,删除在SaveBook方法中创建新连接的行.
public void SaveBook()
{
// This line defines a local variable with the same name of the global one
// It hides the global, if you expect your client to provide the connection
// then remove the line
SqlConnection _sqlcon = new SqlConnection();
....
Run Code Online (Sandbox Code Playgroud)
我觉得这个设计不太明智.这是一个大黄蜂的巢已经开始以各种可能的方式刺痛你.
模型(Book)不应该关注如何将其保存到最终存储(或从中加载),无论是数据库表,XML文件,远程服务还是将来需要做的任何事情.
相反,处理数据库事务的专用类应该具有从Database表中保存和检索Book数据的所有代码(在本例中)
我会用一个名为BookDB的类来设计它
public class BookDB
{
public bool Save(Book obj)
{
using(SqlConnection con = RepositoryUtility.GetConnection())
{
// here code to check and save the object
....
}
}
public Book LoadByKey(int bookID)
{
using(SqlConnection con = RepositoryUtility.GetConnection())
{
Book aBook = new Book();
// here goes the code to load a book from the db using the primarykey
....
return aBook
}
}
... other db methods based ....
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
141 次 |
| 最近记录: |