نوشته شده توسط
iranmsb
میشه یک نمونه از برنامه تون رو بزارید اینجا ؟
شاید بشه مشکل رو حل کرد.
این یه مدل با Linq :
DataClasses1DataContext db = new DataClasses1DataContext();
Book book = new Book();
book.author = textAuthor.Text;
book.name = textBook.Text;
book.publisher = textPublisher.Text;
book.ISBN = textISBN.Text;
book.yearPublished = comboBox1.SelectedIndex;
book.subject = 1;
db.Books.InsertOnSubmit(book);
try
{
db.SubmitChanges();
MessageBox.Show("مشخصات کتاب جدید ثبت شد");
}
catch{
MessageBox.Show("در هنگام ثبت کتاب خطایی رخ داده است.");
}
این هم یه نمونه به روش قدیمی :
using (SqlConnection con = newSqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector y|\Library.mdf;
Integrated Security=True;User Instance=True"))
{
con.Open();
SqlCommand cmd = newSqlCommand(@"insert into Books(name, author, publisher, ISBN, yearPublished, subject)
values(@name, @author, @publisher, @ISBN, @year, @subject)", con);
cmd.Parameters.AddWithValue("@name", textBook.Text);
cmd.Parameters.AddWithValue("@author", textAuthor.Text);
cmd.Parameters.AddWithValue("@publisher", textPublisher.Text);
cmd.Parameters.AddWithValue("@ISBN", textISBN.Text);
cmd.Parameters.AddWithValue("@year", comboBox1.SelectedIndex);
cmd.Parameters.AddWithValue("@subject", 1);
try
{
cmd.ExecuteScalar();
MessageBox.Show("مشخصات کتاب جدید ثبت شد");
}
catch { MessageBox.Show("در هنگام ثبت کتاب خطایی رخ داده است."); }
finally
{
cmd.Dispose();
cmd = null;
}
}