日期:2012-06-24  浏览次数:20496 次

SQL provides statements to create new databases and database objects. You can execute these statements from your program to create databases programmatically. In this article, I抣l show you how to create a new SQL Server database and its objects such as table, stored procedures, views and add and view data. I抣l also show you how to change database table schema programmatically. You抣l see how SQL statement ALTER TABLE is useful when you need to change a database table schema programmatically.  
Not only that, this article also shows you how to view contents from a database table, stored procedures and views.Mahesh Chand is the author of A Programmer's Guide to ADO.NET in C#
by Mahesh ChandSOFTCOVER, 810 PAGES
ISBN: 1893115399Published: March 28, 2002.Price: $49.95 See more details and contents of the book here...

SQL not only let you select, add, and delete data from databases table, it also provides commands to manage databases. Using SQL statements you can create database objects programmatically such as a table, view, stored procedure, rule, index and so on. It also provides commands to alter a database and database schemas for example adding and deleting a column from a database table, adding some constraints to a column and so on. This example shows you how to create a new database table, add data to it, create a view of the data, alter database table and then delete the newly created table.
In this application, I抣l create a SQL Server database, create a database table, add data to it, create database objects such as views, stored procedures, rules, and index and view data in the data grid using Sql data provider.
To test this application, create a Widows application add a data grid control and some button controls. You can even test code by adding only one button or one button for each activity. Our application form looks like Figure 1.

Figure 1. Creating a database and it抯 object application.
After adding controls, add the following variables in the beginning of the form class. private string ConnectionString ="Integrated Security=SSPI;" +
            "Initial Catalog=;" +
            "Data Source=localhost;";
private SqlDataReader reader = null;
private SqlConnection conn = null;
private SqlCommand cmd = null;
private System.Windows.Forms.Button AlterTableBtn;
private string sql = null;
private System.Windows.Forms.Button CreateOthersBtn;
private System.Windows.Forms.Button button1;
First thing I抦 going to do is create ExecuteSQLStmt method. This method executes a SQL statement against the SQL Sever database (mydb which I will create from my program) using Sql data providers using ExecuteNonQuery method. The ExecuteSQLStmt method is listed in Listing 1.
Listing 1. The ExecuteSQLStmt method.  
    private void ExecuteSQLStmt(string sql)
        {
            // Open the connection
            if( conn.State == ConnectionState.Open)
                conn.Close();  
            ConnectionString ="Integrated Security=SSPI;" +