visual| data Two. To insert a record into a SQL Server database with Visual C #:
(1) The main difference between using Visual C # to add records to Access 2000 and SQL Server is to use a different database engine. Before writing a program, assume that the database server name is: Server1, the database name to be accessed is: data1, and the datasheet name is: Books. The user name is: SA. The data structure of the datasheet is the same as the structure of the table in Access 2000. The following is the data Engine program code that opens SQL Server in the program:
Sets the data connection string, which means that the SQL Server database is opened, the server name is Server1, and the database is Data1
String Strcon = "Provider = sqloledb.1;" Persist security Info = False; User ID = sa; Initial Catalog = data1; Data Source = Server1 ";
OleDbConnection myconn = new OleDbConnection (Strcon);
MyConn.Open ();
(2). Source program code (add02.cs) for inserting records into a SQL SERVER database using Visual C #:
Using System;
Using System.Drawing;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data.OleDb;
Using System.Data;
Namespaces used in the import program
public class Dataadd:form {
Private Button Lastrec;
Private Button Nextrec;
Private Button Previousrec;
Private Button Firstrec;
Private Container components;
Private Label title;
Private Button t_new;
Private Button Save;
Private TextBox T_bookstock;
Private TextBox T_bookprice;
Private TextBox T_bookauthor;
Private TextBox T_booktitle;
Private TextBox T_bookid;
Private Label L_bookstock;
Private Label L_bookprice;
Private Label L_bookauthor;
Private Label L_booktitle;
Private Label L_bookid;
Private DataSet myDataSet;
Private BindingManagerBase Mybind;
Define the components to use in your program
Public Dataadd () {
Connect to a database
Getconnected ();
Initialize the content that is required in the form
InitializeComponent ();
}
Release the resources used by the program
public override void Dispose () {
Base. Dispose ();
Components. Dispose ();
}
public static void Main () {
Application.Run (New Dataadd ());
}
public void getconnected ()
{
try{
Sets the data connection string, which means that the SQL Server database is opened, the server name is Server1, the database is data1, and the user name is SA.
String Strcon = "Provider = sqloledb.1;" Persist security Info = False; User ID = sa; Initial Catalog = data1; Data Source = Server1 ";
OleDbConnection myconn = new OleDbConnection (Strcon);
MyConn.Open ();
String strcom = "SELECT * from Books";
Create a DataSet
myDataSet = new DataSet ();
Using OleDbDataAdapter to get a dataset
OleDbDataAdapter mycommand = new OleDbDataAdapter (strcom, myconn);
Bind a dataset to books data tables
Mycommand.fill (myDataSet, "books");
Close this OleDbConnection
Myconn.close ();
}
catch (Exception e)
{
MessageBox.Show ("Connection Error!" + e.tostring (), "error");
}
}
private void InitializeComponent ()
{
components = new System.ComponentModel.Container ();
Nextrec = new Button ();
Lastrec = new Button ();
Previousrec = new Button ();
Firstrec = new Button ();
T_bookprice = new TextBox ();
L_booktitle = new Label ();
L_bookprice = new Label ();
L_bookauthor = new Label ();
T_bookid = new TextBox ();
Save = new Button ();
title = new Label ();
T_bookauthor = new TextBox ();
T_booktitle = new TextBox ();
T_new = new Button ();
L_bookstock = new Label ();
T_bookstock = new TextBox ();
L_bookid = new Label ();
The following is an initialization of the four buttons for data browsing
Firstrec. Location = new System.Drawing.Point (65, 312);
Firstrec. ForeColor = System.Drawing.Color.Black;
Firstrec. Size = new System.Drawing.Size (40, 24);
Firstrec. Font = new System.Drawing.Font ("imitation", 8f);
Firstrec. Text = "First record";
Firstrec. Click + + new System.EventHandler (Gofirst);
Previousrec. Location = new System.Drawing.Point (135, 312);
Previousrec. ForeColor = System.Drawing.Color.Black;
Previousrec. Size = new System.Drawing.Size (40, 24);
Previousrec. Font = new System.Drawing.Font ("imitation", 8f);
Previousrec. Text = "previous One";
Previousrec. Click + + new System.EventHandler (goprevious);
Nextrec. Location = new System.Drawing.Point (205, 312);
Nextrec. ForeColor = System.Drawing.Color.Black;
Nextrec. Size = new System.Drawing.Size (40, 24);
Nextrec. Font = new System.Drawing.Font ("imitation", 8f);
Nextrec. Text = "Next Bar";
Nextrec. Click + + new System.EventHandler (GoNext);
Lastrec. Location = new System.Drawing.Point (275, 312);
Lastrec. ForeColor = System.Drawing.Color.Black;
Lastrec. Size = new System.Drawing.Size (40, 24);
Lastrec. Font = new System.Drawing.Font ("imitation", 8f);
Lastrec. Text = "tail record";
Lastrec. Click + + new System.EventHandler (golast);
The following is an initialization of the display label
L_bookid. Location = new System.Drawing.Point (24, 56);
L_bookid. Text = "Book Serial number:";
L_bookid. Size = new System.Drawing.Size (112, 20);
L_bookid. Font = new System.Drawing.Font ("imitation", 10f);
L_bookid. TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
L_booktitle. Location = new System.Drawing.Point (24, 108);
L_booktitle. Text = "title:";
L_booktitle. Size = new System.Drawing.Size (112, 20);
L_booktitle. Font = new System.Drawing.Font ("imitation", 10f);
L_booktitle. TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
L_bookprice. Location = new System.Drawing.Point (24, 212);
L_bookprice. Text = "Price:";
L_bookprice. Size = new System.Drawing.Size (112, 20);
L_bookprice. Font = new System.Drawing.Font ("imitation", 10f);
L_bookprice. TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
L_bookstock. Location = new System.Drawing.Point (24, 264);
L_bookstock. Text = "Bookshelf number:";
L_bookstock. Size = new System.Drawing.Size (112, 20);
L_bookstock. Font = new System.Drawing.Font ("imitation", 10f);
L_bookstock. TabIndex = 16;
L_bookstock. TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
L_bookauthor. Location = new System.Drawing.Point (24, 160);
L_bookauthor. Text = "Author:";
L_bookauthor. Size = new System.Drawing.Size (112, 20);
L_bookauthor. Font = new System.Drawing.Font ("imitation", 10f);
L_bookauthor. TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
Title. Location = new System.Drawing.Point (32, 16);
Title. Text = "Use vsiual C # to increase data logging!" " ;
Title. Size = new System.Drawing.Size (336, 24);
Title. ForeColor = System.Drawing.Color.Green;
Title. Font = new System.Drawing.Font ("imitation", 14f, System.Drawing.FontStyle.Bold);
The following is an initialization of the labels and text boxes that are set up to display data records and bind the records to a different bound to the text box "text" Property
T_bookid. Location = new System.Drawing.Point (184, 56);
T_bookid. Size = new System.Drawing.Size (80, 20);
T_bookid. Databindings.add ("Text", myDataSet, "Books.bookid");
T_bookstock. Location = new System.Drawing.Point (184, 264);
T_bookstock. Size = new System.Drawing.Size (80, 20);
T_bookstock. Databindings.add ("Text", myDataSet, "Books.bookstock");
T_booktitle. Location = new System.Drawing.Point (184, 108);
T_booktitle. Size = new System.Drawing.Size (176, 20);
T_booktitle. Databindings.add ("Text", myDataSet, "Books.booktitle");
T_bookprice. Location = new System.Drawing.Point (184, 212);
T_bookprice. Size = new System.Drawing.Size (80, 20);
T_bookprice. Databindings.add ("Text", myDataSet, "Books.bookprice");
T_bookauthor. Location = new System.Drawing.Point (184, 160);
T_bookauthor. Size = new System.Drawing.Size (128, 20);
T_bookauthor. Databindings.add ("Text", myDataSet, "Books.bookauthor");
T_new. Location = new System.Drawing.Point (62, 354);
T_new. Size = new System.Drawing.Size (96, 32);
T_new. Text = "new Record";
T_new. Click + + new System.EventHandler (T_newclick);
Save. Location = new System.Drawing.Point (222, 354);
Save. Size = new System.Drawing.Size (96, 32);
Save. TabIndex = 4;
Save. Text = "Save Record";
Save. Click + + new System.EventHandler (Saveclick);
This. Text = "Use vsiual C # to increase the data logging of the program window!" " ;
This. AutoScaleBaseSize = new System.Drawing.Size (5, 13);
This. FormBorderStyle = FormBorderStyle.Fixed3D;
This. ClientSize = new System.Drawing.Size (390, 400);
Add the following components to the form
This. Controls.Add (LASTREC);
This. Controls.Add (NEXTREC);
This. Controls.Add (PREVIOUSREC);
This. Controls.Add (FIRSTREC);
This. Controls.Add (title);
This. Controls.Add (t_new);
This. Controls.Add (save);
This. Controls.Add (T_bookstock);
This. Controls.Add (T_bookprice);
This. Controls.Add (T_bookauthor);
This. Controls.Add (T_booktitle);
This. Controls.Add (T_bookid);
This. Controls.Add (L_bookstock);
This. Controls.Add (L_bookprice);
This. Controls.Add (L_bookauthor);
This. Controls.Add (L_booktitle);
This. Controls.Add (L_bookid);
Bind the object dataset and the Books data table to this Mybind object
Mybind= this. BindingContext [myDataSet, "books"];
}
protected void Saveclick (object sender, System.EventArgs e)
{
Try
{
To determine if all fields have been added, execute when finished, or pop-up prompts
if (T_bookid. Text!= "" && t_booktitle. Text!= "" && t_bookauthor. Text!= "" && t_bookprice. Text!= "" && t_bookstock. Text!= "")
{
Sets the data connection string, which means that the SQL Server database is opened, the server name is Server1, the database is data1, and the user name is SA.
String strconn = "Provider = sqloledb.1;" Persist security Info = False; User ID = sa; Initial Catalog = datal; Data Source = Server1 ";
OleDbConnection myconn = new OleDbConnection (strconn);
MyConn.Open ();
String Strinsert = "INSERT into books (BookID, BookTitle, Bookauthor, Bookprice, Bookstock) VALUES (";
Strinsert + = T_bookid. Text + ", '";
Strinsert + = T_booktitle. Text + "', '";
Strinsert + = T_bookauthor. Text + "',";
Strinsert + = T_bookprice. Text + ",";
Strinsert + = T_bookstock. Text + ")";
OleDbCommand Inst = new OleDbCommand (Strinsert, myconn);
Inst. ExecuteNonQuery ();
Myconn.close ();
}
Else
{
MessageBox.Show ("All field values must be filled!") "," the mistake! " ) ;
}
}
catch (Exception ed)
{
MessageBox.Show ("Save data Record Occurrence" + ed.) ToString (), "Error!" " ) ;
}
}
protected void T_newclick (object sender, System.EventArgs e)
{
T_bookid. Text = "";
T_booktitle. Text = "";
T_bookauthor. Text = "";
T_bookprice. Text = "";
T_bookstock. Text = "";
}
Button "Tail Record" object event program
protected void Golast (object sender, System.EventArgs e)
{
Mybind.position = mybind.count-1;
}
Button "Next" Object event program
protected void GoNext (object sender, System.EventArgs e)
{
if (mybind.position = = mybind.count-1)
MessageBox.Show ("It's the last record!") " ) ;
Else
Mybind.position + 1;
}
Button "Previous" Object event program
protected void Goprevious (object sender, System.EventArgs e)
{
if (mybind.position = 0)
MessageBox.Show ("It's the first record!") " ) ;
Else
Mybind.position-= 1;
}
Button "First record" object event program
protected void Gofirst (object sender, System.EventArgs e)
{
mybind.position = 0;
}
}
Three Summarize:
This article is primarily a two-program example to specify how Visual C # Inserts records into the remote database--sql server and the local database-Access 2000. For other types of databases can be compared to these two two databases to handle, in general, the use of Visual C # processing database only in the selection of the database engine has a big difference in the program's specific design and processing, or very similar. Finally, hopefully this article will help you with your database programming.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.