设为首页 收藏本站
查看: 818|回复: 0

[经验分享] C# :创建SQL Server数据库、设置SQL Server数据库为只读状态、修改和压缩SQL Server数据库、新建(删除和修改)数据表、修改(新增和删

[复制链接]

尚未签到

发表于 2015-7-1 08:43:05 | 显示全部楼层 |阅读模式
DSC0000.gif

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {//创建SQL Server数据库
            string MySQL = "use master;" +
            "IF DB_ID(N'MyDatabase') IS NOT NULL " +
            "DROP DATABASE MyDatabase;" +
            "CREATE DATABASE MyDatabase " +
            "ON(NAME=MyDatabase_dat,FILENAME=\"C:\\MyDatabase.mdf\",SIZE=5,MAXSIZE=10,FILEGROWTH=1) " +
            "LOG ON(NAME=MyDatabase_log,FILENAME=\"C:\\MyDatabase.ldf\",SIZE=2,MAXSIZE=5,FILEGROWTH=1)";        
            SqlConnection MyConnection = new SqlConnection("Data Source=.;Initial Catalog=;Integrated Security=True");
            SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
            try
            {
                MyCommand.Connection.Open();
                MyCommand.ExecuteNonQuery();
                MessageBox.Show("成功创建数据库", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                MyConnection.Close();
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {//设置SQL Server数据库为只读状态
            string MySQL = "use master; " +
                  "IF DB_ID(N'MyDatabase') IS NOT NULL " +
                "EXEC sp_dboption 'MyDatabase', 'read only', 'TRUE'";
              // "EXEC sp_dboption 'MyDatabase', 'read only', 'FALSE'";
            SqlConnection MyConnection = new SqlConnection("Data Source=.;Initial Catalog=;Integrated Security=True");
            SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
            try
            {
                MyCommand.Connection.Open();               
                MyCommand.ExecuteNonQuery();
                MessageBox.Show("设置MyDatabase数据库为只读状态操作成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                MyConnection.Close();
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {//设置SQL Server数据库为脱机状态
            string MySQL = "use master; " +
                  "IF DB_ID(N'MyDatabase') IS NOT NULL " +
                "EXEC sp_dboption 'MyDatabase', 'offline', 'TRUE'";
             //  "EXEC sp_dboption 'MyDatabase', 'offline', 'false'";
            SqlConnection MyConnection = new SqlConnection("Data Source=.;Initial Catalog=;Integrated Security=True");
            SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
            try
            {
                MyCommand.Connection.Open();
                MyCommand.ExecuteNonQuery();
                MessageBox.Show("设置MyDatabase数据库为脱机状态操作成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                MyConnection.Close();
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {//修改SQL Server数据库
            string MySQL = "use master;" +
            "IF DB_ID(N'MyDatabase') IS NOT NULL " +
            "ALTER DATABASE MyDatabase MODIFY FILE(NAME=MyDatabase_dat,SIZE=20)";
            SqlConnection MyConnection = new SqlConnection("Data Source=.;Initial Catalog=;Integrated Security=True");
            SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
            try
            {
                MyCommand.Connection.Open();
                MyCommand.ExecuteNonQuery();
                MessageBox.Show("成功修改数据库的文件尺寸", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                MyConnection.Close();
            }
        }
        private void button5_Click(object sender, EventArgs e)
        {//压缩SQL Server数据库
            string MySQL = "use master;" +
            "IF DB_ID(N'MyDatabase') IS NOT NULL " +
            "DBCC SHRINKDATABASE (MyDatabase, 90) ";
            SqlConnection MyConnection = new SqlConnection("Data Source=.;Initial Catalog=;Integrated Security=True");
            SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
            try
            {
                MyCommand.Connection.Open();
                MyCommand.ExecuteNonQuery();
                MessageBox.Show("成功压缩数据库", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                MyConnection.Close();
            }
        }
        private void button6_Click(object sender, EventArgs e)
        {//在数据库中新建数据表
            string MySQL ="IF OBJECT_ID(N'MyDatabase..商品清单', N'U') IS NOT NULL "+
                 "DROP TABLE 商品清单;" +
                "CREATE TABLE 商品清单 ("+
                "[货号] [char] (14) NOT NULL Primary Key,"+
                "[条码] [char] (14) NULL ,"+
                "[拼音编码] [char] (40) NULL,"+
                "[品名] [varchar] (80) NULL ,"+
                "[规格] [varchar] (40) NULL ,"+
                "[单位] [char] (6) NOT NULL ,"+
                "[产地] [varchar] (50) NULL ,"+
                "[类别] [char] (20) NULL ,"+
                "[进货价] [decimal] (28,6) NULL default(0),"+
                "[销售价1] [decimal] (28,6) NULL default(0),"+
                "[销售价2] [decimal] (28,6) NULL default(0),"+
                "[最低售价] [decimal] (28,6) NULL default(0))";      
            SqlConnection MyConnection = new SqlConnection("Data Source = .;Database = MyDatabase;Integrated Security=SSPI");
            SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
            try
            {
                MyCommand.Connection.Open();
                MyCommand.ExecuteNonQuery();
                MessageBox.Show("成功在MyDatabase数据库中创建数据表", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                MyConnection.Close();
            }
        }
        private void button7_Click(object sender, EventArgs e)
        {//在数据库中删除数据表
            string MySQL = "IF OBJECT_ID(N'MyDatabase..商品清单', N'U') IS NOT NULL " +
                 "DROP TABLE 商品清单;";            
            SqlConnection MyConnection = new SqlConnection("Data Source = .;Database = MyDatabase;Integrated Security=SSPI");
            SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
            try
            {
                MyCommand.Connection.Open();
                MyCommand.ExecuteNonQuery();
                MessageBox.Show("成功在MyDatabase数据库中删除数据表", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                MyConnection.Close();
            }
        }
        private void button8_Click(object sender, EventArgs e)
        {//在数据表中修改数据列
            //"[产地] [varchar] (50) NULL ,"
            string MySQL =  "ALTER TABLE 商品清单 ALTER COLUMN [产地] [char](100) NOT NULL;";               
            SqlConnection MyConnection = new SqlConnection("Data Source = .;Database = MyDatabase;Integrated Security=SSPI");
            SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
            try
            {
                MyCommand.Connection.Open();
                MyCommand.ExecuteNonQuery();
                MessageBox.Show("成功在“商品清单”数据表中修改数据列", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                MyConnection.Close();
            }
        }
        private void button9_Click(object sender, EventArgs e)
        {//在数据表中添加数据列
            string MySQL = "ALTER TABLE 商品清单 ADD [检验员] [varchar] (50) NULL;";
            SqlConnection MyConnection = new SqlConnection("Data Source = .;Database = MyDatabase;Integrated Security=SSPI");
            SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
            try
            {
                MyCommand.Connection.Open();
                MyCommand.ExecuteNonQuery();
                MessageBox.Show("成功在“商品清单”数据表中添加数据列", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                MyConnection.Close();
            }
        }
        private void button10_Click(object sender, EventArgs e)
        {//在数据表中删除数据列
            string MySQL = "ALTER TABLE 商品清单 DROP COLUMN [检验员] ;";
            SqlConnection MyConnection = new SqlConnection("Data Source = .;Database = MyDatabase;Integrated Security=SSPI");
            SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
            try
            {
                MyCommand.Connection.Open();
                MyCommand.ExecuteNonQuery();
                MessageBox.Show("成功在“商品清单”数据表中删除数据列", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                MyConnection.Close();
            }
        }
        private void button11_Click(object sender, EventArgs e)
        {//删除指定数据表中的所有记录
            string MySQL = "TRUNCATE TABLE 商品清单;";
            SqlConnection MyConnection = new SqlConnection("Data Source = .;Database = MyDatabase;Integrated Security=SSPI");
            SqlCommand MyCommand = new SqlCommand(MySQL, MyConnection);
            try
            {
                MyCommand.Connection.Open();
                MyCommand.ExecuteNonQuery();
                MessageBox.Show("成功在“MyDatabase”数据库中删除“商品清单”数据表的所有记录", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                MyConnection.Close();
            }
        }
    }
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-82117-1-1.html 上篇帖子: C# 附加数据库方法(将SQL Server 数据库文件附加至SQL Server实例) 下篇帖子: Sql Server Alter语句
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表