Summary: In this article, we demonstrated how to use the Ado.net method in C # Builder personal to connect to an MSDE database, create a table, insert and select Records, and then display records in a data grid (DataGrid).
C # Builder A unique feature is that you can use the Borland Data Providers for ADO. NET is designed to support connecting and manipulating databases, specific drivers for DB2, InterBase, Oracle, SQL Server/msde, and the potential to add Third-party drivers such as dbexpress. However, C # Builder personal does not carry Borland Data Providers, which means that you can only use the "simplest" ADO. net method.
In this article, I'll show you how to use C # Builder personal to combine with a simple ado.net, connect to an MSDE database, create a table, insert records, select records from this table, and then display them in a data grid (DataGrid).
I assume you have MSDE installed (C # Builder, including personal version).
SqlConnection
Start C # Builder, and click File | new-c# application to start a new project. Place a SqlConnection control and set the Connection property to
Data Source=.; Initial Catalog=master; Integrated security=SSPI
Note that master is a database with MSDE, but you can use any other database or. NET database management system (it is necessary to modify ConnectionString).
Place a button on the WinForm, set the title "Connect", and enter the following code in the Button_Click event handler to open the SqlConnection (note that I set the ConnectionString property again. But this time it's just about how to set it in "pure" in code.
private void button1_Click(object sender, System.EventArgs e)
{
try
{
sqlConnection1.ConnectionString = "Data Source=.; Initial Catalog=master; Integrated security=SSPI";
sqlConnection1.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
This is the first Test and is the easiest to fail (all the other steps are easy). If an exception occurs, you must modify the ConnectionString to determine that it contains the correct reference (note: Of course, make sure that the SQL Server/msde is running).
SqlCommand
Once you can create a successful connection, it's time to place the SqlCommand control. This is a control that can be used to execute a command: An SQL statement. You can use object Inspector to indicate that the connection property is a SqlConnection control and to place the command that is executed in the CommandText property. Note the Drop-down combobox for the Connection property does not look like a list of SqlConnection controls, but the trick is to double-click the property value (which lists all possible values). Another method is to enter the name of the control you want to use, so "s" will immediately select "SqlConnection1". To clear the Connection property again, you need to type "("), which will cause the "(none)" to appear again.
Because it is very useful to see how you can do these things with "pure" code, I will now specify all the required property values (including the SQL command I want to execute) in the source code.
So, place another button, set the title "SQL", and write the following code in the Button_Click event handler to create a table (if the table exists, delete it first) and insert three records:
private void button2_Click(object sender, System.EventArgs e)
{
try
{
sqlCommand1.Connection = sqlConnection1;
sqlCommand1.CommandText = "drop table test42";
try
{
sqlCommand1.ExecuteNonQuery(); // 创建表
}
catch {}; // 忽略
sqlCommand1.CommandText = "create table test42 (id int NOT NULL, name nvarchar(42))";
sqlCommand1.ExecuteNonQuery(); //创建表
sqlCommand1.CommandText = "insert into test42 values(1, 'Bob Swart')";
sqlCommand1.ExecuteNonQuery(); //插入表
sqlCommand1.CommandText = "insert into test42 values(2, 'Erik Mark Pascal Swart')";
sqlCommand1.ExecuteNonQuery(); //插入表
sqlCommand1.CommandText = "insert into test42 values(3, 'Natasha Louise Delphine Swart')";
sqlCommand1.ExecuteNonQuery(); // 插入表
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}