C#如何创建SQLServer表(新手)

chenleilei0001 2011-04-07 07:37:02
使用vs2005编C#程序,怎么建SQLServer表??

如何连接数据库,

如何建表,

如何读取数据并显示出来。

最好有个小程序!
...全文
742 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
风之影子 2011-04-08
  • 打赏
  • 举报
回复
http://download.csdn.net/source/1698509

去年无聊的时候写的,里面有C#操作数据库的大多示例。可做学习。
davidcoffee 2011-04-08
  • 打赏
  • 举报
回复
.......怎么感觉应该去SQL区问~LZ如有需需要学习数据库连接访问和简单的增删改查,可留下LZ您的邮箱给你发个DEMO~
txzsp 2011-04-08
  • 打赏
  • 举报
回复
SqlCommand cmd = new SqlCommand("create table userInfo
(
userId int identity(1,1) not null primary key,
powerId int not null,
userName varchar(10) not null,
pwd varchar(13),
)", conn);
shanyilan 2011-04-08
  • 打赏
  • 举报
回复
首先,你机子上必须安装了vs2005和sql2005,然后你可以在sql中建立数据库,在数据库中建立你所需要的表以及在表中添加你所要用到的数据
在你的程序中,命名空间上添加 using System.Data.SqlClient;
添加一个类
using System;
using System.Collections.Generic;
using System.Text;

namespace BookStore
{
class Dbconfig
{

public static string CString = "Server=notebook;Database=bookstore;Uid=sa;Pwd=123;";
}
}
其中server是服务器名,database是自己建的数据库,uid是登陆名,pwd是密码
你在程序中,所需要的数据是从这里调出的

在代码编写中
SqlConnection conn = new SqlConnection(Dbconfig.CString);
SqlCommand comm = new SqlCommand();
cjh200102 2011-04-08
  • 打赏
  • 举报
回复
楼主多看下书就知道了
esebf01 2011-04-08
  • 打赏
  • 举报
回复
create database silenceBBS

on

(

name='silence_bbs',

filename='D:\silence\bbs.mdf',

size='300',

maxsize='20480',

filegrowth='50'

)

log on

(

name='silen_bbs',

filename='D:\silcence\bbs.ldf',

size='100',

maxsize='1500',

filegrowth='100'

)

go
刘婷婷 2011-04-08
  • 打赏
  • 举报
回复
安装sql2005或2008,然后只在数据库那点击右键-新建表-设置字段,类型,保存就可以啦
代码创建:
create table 表名
(
--这里写字段,类型
)
go

连接数据库,在你的项目配置文件web.config里的如下节点里写
<appSettings>
<add key="dbConnection" value="User ID=数据库登录用户名;Password=登录密码;Initial Catalog=数据库名;Data Source=服务器名(如果是本地就是你的计算机名或点号也行)"/>
</appSettings>

在后台获取配置文件的连接字符串
SqlConnection connestr = new SqlConnection(ConfigurationManager.appSettings["dbConnection"]);
connestr.Open();
string sql = "insert into PARTNER_C01(PARTNER_TYPE_DESC) values ('" + name + "')";//你自己的sql语句
SqlCommand com = new SqlCommand(sql,connestr);
com.CommandType = CommandType.Text;
int num = com.ExecuteNonQuery();
connestr.Close();
if(num>0)
{
提示;
}

linuxjava01 2011-04-08
  • 打赏
  • 举报
回复
找一本基础书看看关于ADO.NET那一块。
kingdom_0 2011-04-08
  • 打赏
  • 举报
回复
VS2008自带sql2005数据库,你可以再安装一个客户端即可
在项目中,找到数据源选项,可以建立数据库。
也可以在企业管理器中建立数据库表
monami1324 2011-04-07
  • 打赏
  • 举报
回复
VS2005里面的目录里 点数据→添加新数据源 接下来你自己摸索吧
我也是新手,弄过几次,慢慢就会了的
Linking 2011-04-07
  • 打赏
  • 举报
回复
至于理论楼上已经说了很多了!
下面给个例子吧:
 string connection = "uid=sa;pwd=;Database=T_Linking;server=(local)";
SqlConnection objConnection = new SqlConnection(connection);
objConnection.Open();
DataSet ds=new DataSet();
SqlDataAdapter command = new SqlDataAdapter("select * from con", objConnection);
command.Fill(ds);
ListView3.DataSource = ds;
ListView3.DataBind();

附上一个绑定ListView的完整数据库连接代码,希望对楼主有所帮助!
czs06815394 2011-04-07
  • 打赏
  • 举报
回复
可以在VS里面建数据库、建表 也可以通过ADOX编码的形式来建
j2EEhoulu 2011-04-07
  • 打赏
  • 举报
回复
这是我最近建的表
use master
go
if exists(select * from sysdatabases where name='sellDB')
drop database sellDB
go
create database sellDB
on
(
name='sellDB_data',
filename='e:\sellDB_data.mdf',
filegrowth=15%
)
log on
(
name='sellDB_log',
filename='e:\sellDB_log.ldf'
)
go


--创建userInfo表
use sellDB
go
create table userInfo
(
userId int identity(1,1) not null primary key,
powerId int not null,
userName varchar(10) not null,
pwd varchar(13),
)
go


--品牌表
create table brand
(
brandId int identity(1,1) not null primary key,
brandName varchar(20) not null,
typeName varchar(20) not null
)
go


--付款状态
create table payType
(
payTypeId int identity(1,1) not null primary key,
payTypeName varchar(10) not null
)
go
--客户表
create table customer
(
customerId int identity(1,1) not null primary key,
customerName varchar(20) not null,
customerType varchar(10) not null
)
go
--机子状态
create table phoneState
(
phoneStateId int identity(1,1) not null primary key,
phoneState varchar(20) not null
)
go


--创建trade表
create table trade
(
tradeId int identity(1,1) not null primary key,
userID int foreign key references userinfo(userId),
brandId int foreign key references brand(brandId) not null,
imei varchar(30) not null,
msn varchar(30),
inTime datetime default(getdate())not null,
inPrice float not null,
outTime datetime,
outPrice float,
storage varchar(20) not null,
payPrice float ,
customerId int foreign key references customer(customerId),
payTypeId int default(1) foreign key references payType(payTypeId),
phoneStateId int default(3) foreign key references phoneState(phoneStateId) ,
phoneDe varchar(20),
tradeDe varchar(20)
)
go

create table pay
(
payId int identity(1,1) not null primary key,
customerID int foreign key references customer(customerId),
TotalPrice float,
PayPrice float,
payTime datetime
)

insert into phoneState
select '正常' union
select '故障机' union
select '备机'

select * from paytype
--添加SQL登录账号
exec sp_addlogin 'xiaotu','1234'
--创建数据库用户
exec sp_grantdbaccess 'xiaotu','xiaotu'

grant select,insert,update,delete,select on userinfo to xiaotu
grant select,insert,update,delete,select on brand to xiaotu
grant select,insert,update,delete,select on customer to xiaotu
grant select,insert,update,delete,select on payType to xiaotu
grant select,insert,update,delete,select on phoneState to xiaotu
grant select,insert,update,delete,select on trade to xiaotu
zhengchengbaishi 2011-04-07
  • 打赏
  • 举报
回复
连接数据库后台代码
String strconn = @"server=计算机名;database=数据库表明;user=数据库用户名;pwd=数据库登录密码";
SqlConnection Sqlcon = new SqlConnection(strconn);
  • 打赏
  • 举报
回复
建议你一下SQL客户端管理工具,在程序里执行语句也是可以的
都是基础,去下几个简单的多看一看就会了
http://www.51aspx.com/Type/1/
快乐大法师 2011-04-07
  • 打赏
  • 举报
回复
1.如果是连库的话就采用我下面的方法……
首先就是数据类的引用
using System.Data;
using System.Data.SqlClient;

public SqlConnection conn= new SqlConnection("Data Source=.;Initial Catalog=db_bookmanage; Integrated Security=SSPI");

SqlConnection就是SQL数据库连接类……实例的时候在后面指定连接字符串,就可以连接到指定的数据库!这里解释一下,Data Source=(填服务器名称,默认一点就可以了);Initial Catalog=(填自定义数据库名称,也就是你要连接的数据库名);
Integrated Security=SSPI(连接方式,不需要管);

2.SQL数据库的操作
conn.Open();//打开连接
Sqlcommand cmd=new Sqlcommand("sql执行语句",conn);
(1)如果"sql执行语句" 是添、删、改
cmd.ExecuteNonQuery();//反回int型,通常以0做为标准进行if判断是否执行成功!如果>0则成功……
最后 conn.Close();//关闭数据库;
(2) 如果"sql执行语句" 是简单的查询语句
SqlDataReader reader = cmd.ExecuteReader();
if(read.read()==true)
{
//......已经读取到记录的结果代码!
}
else
{
//........
}
(3)数据绑定
SqlCommand cmd = new SqlCommand("sql查询语句", conn);
SqlDataAdapter dr = new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
dr.fill(ds,"自定义表名称");
this.需要绑定的数据控件.DataSource=ds.Tables["自定义表名称"];

moonwrite 2011-04-07
  • 打赏
  • 举报
回复
建议楼主下载一个视频看看
比较直观
chenleilei0001 2011-04-07
  • 打赏
  • 举报
回复
装vs2005不是自带sqlserver 2005么?

怎么连接数据库,怎么提取数据啊?
一切为了你 2011-04-07
  • 打赏
  • 举报
回复
先安装sqlserver 2005 建立一个数据库 然后右键 新建一个表 ....

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧