创建使用C#数据库和表 [英] Creating Databases and tables using c#

查看:143
本文介绍了创建使用C#数据库和表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#Windows窗体应用程序创建。
我有几个数据库和表在SQL Server 2005 Express版本。要创建我的项目的设置我希望,而我的设置向导任何远程机器上运行附加数据库和表动态。

I have a windows form application created using C#. I have a few databases and tables in SQL server 2005 express edition. To create a setup of my project I wish to attach the databases and tables dynamically while my setup wizard runs on any remote machine.

我试着写一个代码,就干脆创建数据库,也是一个try-catch语句,如果数据库中存在,但每当我的应用程序运行时可能会出现问题的try catch语句的每次执行,这将检查。

I tried to write a code which will simply create database and also a try-catch statement which will check if the database exists but everytime execution of try catch statement whenever my application runs may be a problem.

这是可能的通过简单的.mdf文件,我的项目安装,我创建了安装向导?

Is this possible by simply attaching the .mdf files with my project as I create the setup wizard ?

请帮忙。
在此先感谢

Please help. Thanks in advance.

推荐答案

要创建一个表:

string createString = "CREATE TABLE myTable (column1 INT, column2 NVARCHAR(10))"; //YOUR SQL COMMAND TO CREATE A TABLE
SqlConnection connection = new SqlConnection(YOUR CONNECTION STRING);

SqlCommand create = new SqlCommand(createString , connection);

connection.Open();
create.ExecuteNonQuery();
connection.Close();

这篇关于创建使用C#数据库和表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆