【导语】“中国风旗袍”通过精心收集,向本站投稿了5篇动态关联表数据库教程,下面是小编整理后的动态关联表数据库教程,希望能帮助到大家!
篇1:动态关联表数据库教程
动态
原帖地址:
community.csdn.net/Expert/topic/3452/3452577.xml?temp=.1377375
--示例数据
create table [table](A sysname,B varchar(10))
insert [table] select 'table_1','a'
union all select 'table_2','b'
create table table_1(A int)
insert table_1 select 1
union all select 2
create table table_2(A int)
insert table_2 select 3
union all select 4
go
/*--问题说明:
table中,A字段存储着其他表的名称
A字段中,记录和各个表的A字段的最大值
即得到如下结果:
table.B C
-------- -----------
a 1
b 2
--*/
--处理方法
declare @s varchar(8000)
set @s=''
select @s=@s+' when '''+A+''' then(select max(A) from ['+A+'])'
from [table] group by A
exec('select B,C=case A'+@s+' end from [table]')
go
--删除测试
drop table [table],table_1,table_2
/*--测试结果
B C
---------- -----------
a 2
b 4
--*/
篇2:动态创建SQL Server数据库、表、存储过程数据库教程
server|创建|存储过程|动态|数据|数据库
下面是利用SQL语句创建数据库、表、存储过程、视图、索引、规则、修改表、查看数据等的方法,所要增加的控件如下:
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
Private ConnectionString As String = “Data Source=.;Initial Catalog=;User Id=sa;Password=;”
Private reader As SqlDataReader = Nothing
Private conn As SqlConnection = Nothing
Private cmd As SqlCommand = Nothing
Private AlterTableBtn As System.Windows.Forms.Button
Private sql As String = Nothing
Private CreateOthersBtn As System.Windows.Forms.Button
#Region “ Windows 窗体设计器生成的代码 ”
'窗体重写处置以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose
End If
End If
MyBase.Dispose(disposing)
End Sub
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Private components As System.ComponentModel.IContainer
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents CreateDBBtn As System.Windows.Forms.Button
Friend WithEvents CreateTableBtn As System.Windows.Forms.Button
Friend WithEvents CreateSPBtn As System.Windows.Forms.Button
Friend WithEvents CreateViewBtn As System.Windows.Forms.Button
Friend WithEvents btnAlterTable As System.Windows.Forms.Button
Friend WithEvents btnCreateOthers As System.Windows.Forms.Button
Friend WithEvents btnDropTable As System.Windows.Forms.Button
Friend WithEvents btnViewData As System.Windows.Forms.Button
Friend WithEvents btnViewSP As System.Windows.Forms.Button
Friend WithEvents btnViewView As System.Windows.Forms.Button
Me.CreateDBBtn = New System.Windows.Forms.Button()
Me.CreateTableBtn = New System.Windows.Forms.Button()
Me.CreateSPBtn = New System.Windows.Forms.Button()
Me.CreateViewBtn = New System.Windows.Forms.Button()
Me.btnAlterTable = New System.Windows.Forms.Button()
Me.btnCreateOthers = New System.Windows.Forms.Button()
Me.btnDropTable = New System.Windows.Forms.Button()
Me.btnViewData = New System.Windows.Forms.Button()
Me.btnViewSP = New System.Windows.Forms.Button()
Me.btnViewView = New System.Windows.Forms.Button()
Me.DataGrid1 = New System.Windows.Forms.DataGrid()
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'CreateDBBtn
'
Me.CreateDBBtn.Location = New System.Drawing.Point(19, 9)
Me.CreateDBBtn.Name = “CreateDBBtn”
Me.CreateDBBtn.Size = New System.Drawing.Size(104, 23)
Me.CreateDBBtn.TabIndex = 0
Me.CreateDBBtn.Text = “创建数据库”
'
'CreateTableBtn
'
Me.CreateTableBtn.Location = New System.Drawing.Point(139, 9)
Me.CreateTableBtn.Name = “CreateTableBtn”
Me.CreateTableBtn.TabIndex = 1
Me.CreateTableBtn.Text = “创建表”
'
'CreateSPBtn
'
Me.CreateSPBtn.Location = New System.Drawing.Point(230, 9)
Me.CreateSPBtn.Name = “CreateSPBtn”
Me.CreateSPBtn.Size = New System.Drawing.Size(104, 23)
Me.CreateSPBtn.TabIndex = 2
Me.CreateSPBtn.Text = “创建存储过程”
'
'CreateViewBtn
'
Me.CreateViewBtn.Location = New System.Drawing.Point(350, 9)
Me.CreateViewBtn.Name = “CreateViewBtn”
Me.CreateViewBtn.TabIndex = 3
Me.CreateViewBtn.Text = “创建视图”
'
'btnAlterTable
'
Me.btnAlterTable.Location = New System.Drawing.Point(441, 9)
Me.btnAlterTable.Name = “btnAlterTable”
Me.btnAlterTable.TabIndex = 4
Me.btnAlterTable.Text = “修改表”
'
'btnCreateOthers
'
Me.btnCreateOthers.Location = New System.Drawing.Point(17, 43)
Me.btnCreateOthers.Name = “btnCreateOthers”
Me.btnCreateOthers.Size = New System.Drawing.Size(104, 23)
Me.btnCreateOthers.TabIndex = 5
Me.btnCreateOthers.Text = “创建规则和索引”
'
'btnDropTable
'
Me.btnDropTable.Location = New System.Drawing.Point(138, 43)
Me.btnDropTable.Name = “btnDropTable”
Me.btnDropTable.TabIndex = 6
Me.btnDropTable.Text = “删除表”
'
'btnViewData
'
Me.btnViewData.Location = New System.Drawing.Point(351, 43)
Me.btnViewData.Name = “btnViewData”
Me.btnViewData.TabIndex = 7
Me.btnViewData.Text = “查看数据”
'
'btnViewSP
'
Me.btnViewSP.Location = New System.Drawing.Point(230, 43)
Me.btnViewSP.Name = “btnViewSP”
Me.btnViewSP.Size = New System.Drawing.Size(104, 23)
Me.btnViewSP.TabIndex = 8
Me.btnViewSP.Text = “查看存储过程”
'
'btnViewView
'
Me.btnViewView.Location = New System.Drawing.Point(443, 43)
Me.btnViewView.Name = “btnViewView”
Me.btnViewView.TabIndex = 9
Me.btnViewView.Text = “查看视图”
'
'DataGrid1
'
Me.DataGrid1.DataMember = “”
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(20, 76)
Me.DataGrid1.Name = “DataGrid1”
Me.DataGrid1.Size = New System.Drawing.Size(500, 183)
Me.DataGrid1.TabIndex = 10
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(538, 281)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.DataGrid1, Me.btnViewView, _
Me.btnViewSP, Me.btnViewData, Me.btnDropTable, Me.btnCreateOthers, Me.btnAlterTable, _
Me.CreateViewBtn, Me.CreateSPBtn, Me.CreateTableBtn, Me.CreateDBBtn})
Me.Name = “Form1”
Me.Text = “动态创建SQL Server数据库、表、存储过程等架构信息”
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
' 创建数据库
Private Sub CreateDBBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles CreateDBBtn.Click
conn = New SqlConnection(ConnectionString)
' 打开连接
If conn.State ConnectionState.Open Then
conn.Open()
End If
'MyDataBase为数据库名称
Dim sql As String = “CREATE DATABASE MyDataBase ON PRIMARY (Name=MyDataBase_data, filename = ” + _
“'D:\\MyDataBase.mdf', size=3,” + “maxsize=5, filegrowth=10%) log on” + “(name=MyDataBase_log, ” + _
“filename='D:\\MyDataBase.ldf',size=3,” + “maxsize=20,filegrowth=1)”
cmd = New SqlCommand(sql, conn)
Try
cmd.ExecuteNonQuery()
Catch ae As SqlException
MessageBox.Show(ae.Message.ToString())
End Try
End Sub
'创建表
Private Sub CreateTableBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles CreateTableBtn.Click
conn = New SqlConnection(ConnectionString)
' 打开连接
If conn.State = ConnectionState.Open Then
conn.Close()
End If
ConnectionString = “Data Source=.;Initial Catalog=MyDataBase;User Id=sa;Password=;”
conn.ConnectionString = ConnectionString
conn.Open()
sql = “CREATE TABLE myTable” + “(myId INTEGER CONSTRAINT PKeyMyId PRIMARY KEY,” + _
“myName CHAR(50) NOT Null, myAddress CHAR(255), myValues FLOAT)”
cmd = New SqlCommand(sql, conn)
Try
cmd.ExecuteNonQuery()
' 添加纪录
sql = “INSERT INTO myTable(myId, myName, myAddress, myValues) ” + _
“VALUES (1001, _'【孟宪会之精彩世界】之一', 'xml.sz.luohuedu.net/', 100 ) ”
cmd = New SqlCommand(sql, conn)
cmd.ExecuteNonQuery()
sql = “INSERT INTO myTable(myId, myName, myAddress, myValues) ” + _
“VALUES (1002, '【孟宪会之精彩世界】之二', 'www.erp800.com/net_lover/', 99) ”
cmd = New SqlCommand(sql, conn)
cmd.ExecuteNonQuery()
sql = “INSERT INTO myTable(myId, myName, myAddress, myValues) ” + _
“VALUES (1003, '【孟宪会之精彩世界】之三', 'xml.sz.luohuedu.net/', 99) ”
cmd = New SqlCommand(sql, conn)
cmd.ExecuteNonQuery()
sql = “INSERT INTO myTable(myId, myName, myAddress, myValues) ” + _
“VALUES (1004, '【孟宪会之精彩世界】之四', 'www.erp800.com/net_lover/', 100) ”
cmd = New SqlCommand(sql, conn)
cmd.ExecuteNonQuery()
Catch ae As SqlException
MessageBox.Show(ae.Message.ToString())
End Try
End Sub
'创建存储过程
Private Sub CreateSPBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles CreateSPBtn.Click
sql = “CREATE PROCEDURE myProc AS” + “ SELECT myName, myAddress FROM myTable GO”
ExecuteSQLStmt(sql)
End Sub
'创建视图
Private Sub CreateViewBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles CreateViewBtn.Click
sql = “CREATE VIEW myView AS SELECT myName FROM myTable”
ExecuteSQLStmt(sql)
End Sub
'修改表
Private Sub btnAlterTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnAlterTable.Click
sql = “ALTER TABLE MyTable ADD newCol datetime NOT NULL DEFAULT (getdate())”
ExecuteSQLStmt(sql)
End Sub
'创建规则和索引
Private Sub btnCreateOthers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnCreateOthers.Click
sql = “CREATE UNIQUE INDEX ” + “myIdx ON myTable(myName)”
ExecuteSQLStmt(sql)
sql = “CREATE RULE myRule ” + “AS @myValues >= 90 AND @myValues < 9999”
ExecuteSQLStmt(sql)
End Sub
'删除表
Private Sub btnDropTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnDropTable.Click
Dim sql As String = “DROP TABLE MyTable”
ExecuteSQLStmt(sql)
End Sub
'浏览表数据
Private Sub btnViewData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnViewData.Click
conn = New SqlConnection(ConnectionString)
If conn.State = ConnectionState.Open Then
conn.Close()
End If
ConnectionString = “Data Source=.;Initial Catalog=MyDataBase;User Id=sa;Password=;”
conn.ConnectionString = ConnectionString
conn.Open()
Dim da As New SqlDataAdapter(“SELECT * FROM myTable”, conn)
Dim ds As New DataSet(“myTable”)
da.Fill(ds, “myTable”)
DataGrid1.DataSource = ds.Tables(“myTable”).DefaultView
End Sub
'浏览存储过程
Private Sub btnViewSP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnViewSP.Click
conn = New SqlConnection(ConnectionString)
If conn.State = ConnectionState.Open Then
conn.Close()
End If
ConnectionString = “Data Source=.;Initial Catalog=MyDataBase;User Id=sa;Password=;”
conn.ConnectionString = ConnectionString
conn.Open()
Dim da As New SqlDataAdapter(“myProc”, conn)
Dim ds As New DataSet(“SP”)
da.Fill(ds, “SP”)
DataGrid1.DataSource = ds.DefaultViewManager
End Sub
'浏览视图
Private Sub btnViewView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnViewView.Click
conn = New SqlConnection(ConnectionString)
If conn.State = ConnectionState.Open Then
conn.Close()
End If
ConnectionString = “Data Source=.;Initial Catalog=MyDataBase;User Id=sa;Password=;”
conn.ConnectionString = ConnectionString
conn.Open()
Dim da As New SqlDataAdapter(“SELECT * FROM myView”, conn)
Dim ds As New DataSet()
da.Fill(ds)
DataGrid1.DataSource = ds.DefaultViewManager
End Sub
Private Sub ExecuteSQLStmt(ByVal sql As String)
conn = New SqlConnection(ConnectionString)
' 打开连接
If conn.State = ConnectionState.Open Then
conn.Close()
End If
ConnectionString = “Data Source=.;Initial Catalog=MyDataBase;User Id=sa;Password=;”
conn.ConnectionString = ConnectionString
conn.Open()
cmd = New SqlCommand(sql, conn)
Try
cmd.ExecuteNonQuery()
Catch ae As SqlException
MessageBox.Show(ae.Message.ToString())
End Try
End Sub
End Class
篇3:动态分组查询数据库教程
动态|分组
原帖地址:
community.csdn.net/Expert/topic/3428/3428792.xml?temp=.6476251
--示例数据
create table 表(ID int,NUM int)
insert 表 select 1,2
union all select 2,3
union all select 3,2
union all select 4,2
union all select 5,12
union all select 6,2
union all select 7,1
union all select 8,5
union all select 9,1
go
/*--问题说明:
输入分组参数,比如输入 “3,6” ,实现按 ID<=3,3
输入分组参数,比如输入 “2,5,8” ,实现按 ID<=2,2
--*/
--查询的存储过程
create proc p_qry
@numlist varchar(1000)
as
set nocount on
declare @t table(id int identity,组 varchar(10),a int,b int)
declare @i int,@pnum varchar(10)
select @i=charindex(',',@numlist+',')
,@pnum=left(@numlist,@i-1)
,@numlist=stuff(@numlist,1,@i,'')
,@i=charindex(',',@numlist)
insert @t select 'id<='+@pnum,null,@pnum
while @i>0
begin
insert @t select @pnum+'
select @pnum=left(@numlist,@i-1)
,@numlist=stuff(@numlist,1,@i,'')
,@i=charindex(',',@numlist)
end
insert @t select 'id>'+@numlist,@numlist,null
select b.组,num=sum(a.num)
from 表 a,@t b
where case
when b.a is null then case when a.id<=b.b then 1 else 0 end
when b.b is null then case when a.id>b.a then 1 else 0 end
else case when a.id>b.a and a.id<=b.b then 1 else 0 end
end=1
group by b.组
order by min(b.id)
go
--调用存储过程进行查询
exec p_qry '2,5,8'
go
--删除测试
drop table 表
drop proc p_qry
/*--测试结果
组 num
---------- -----------
id<=2 5
2
id>8 1
--*/
篇4:mysql数据库动态创建表
mysql数据库动态创建表
大家一般可能很少有这种需求吧,我以前也没有遇到过,但这次做项目需要这么做,
就是表的字段名和字段数都不是固定的,要根据需要来创建。
这是我的创建形成过程,大家照着演示一下就知道了我的动态表的来龙去脉了。
第一步.创建相关表
/*---建立所有指标信息的临时表---*/
drop table if exists INTERBANKBONDQUOTE_SClass;
create table INTERBANKBONDQUOTE_SClass (
Name varchar(50) not null,
id int Primary key,
Parent int,
Value varchar(50)
);
insert into INTERBANKBONDQUOTE_SClass(Name,id,Value)values('最新成交',0,'');
insert into INTERBANKBONDQUOTE_SClass(Name,id,Value)values('买入信息',1,'');
insert into INTERBANKBONDQUOTE_SClass(Name,id,Value)values('买卖价差',2,'');
insert into INTERBANKBONDQUOTE_SClass(Name,id,Value)values('卖出信息',3,'');
insert into INTERBANKBONDQUOTE_SClass(Name,id,Parent,Value)values('中债最新估值',4,-1,'');
insert into INTERBANKBONDQUOTE_SClass(Name,id,Value)values('含权债行权指标',5,'');
insert into INTERBANKBONDQUOTE_SClass(Name,id,Value)values('基础指标',6,'');
insert into INTERBANKBONDQUOTE_SClass(Name,id,Value)values('双边报价笔数',7,'');
insert into INTERBANKBONDQUOTE_SClass(Name,id,Parent,Value)values('报价方',8,1,'col4');
insert into INTERBANKBONDQUOTE_SClass(Name,id,Parent,Value)values('报价方会员号',9,1,'col5');
insert into INTERBANKBONDQUOTE_SClass(Name,id,Parent,Value)values('报价时间',10,1,'col6');
insert into INTERBANKBONDQUOTE_SClass(Name,id,Parent,Value)values('匿名',11,1,'col7');
第二步:/*---建立显示数据表格标题的表---*/
drop table if exists INTERBANKBONDQUOTE_T;
create table INTERBANKBONDQUOTE_T as select concat('\\'',a.Name,',',b.Name,'\\' __TITLE__',b.Value);
第三步: 这里有两种实现方法。因为在mysql中,这时的变量长度受到了限制 ,本来应该longtext足够长的,可实际只返回了限制长度的。如果字段太多了,就要用第二种方法。
存储过程A: 这里变量返回值长度受限,字段不多时可以。
DELIMITER $$
DROP PROCEDURE IF EXISTS `dzhappdb_bond`.`INTERBANKBONDQUOTE_TSP`$$
CREATE PROCEDURE INTERBANKBONDQUOTE_TSP
BEGIN
DECLARE objs1 TEXT;
DECLARE objs TEXT;
SELECT GROUP_CONCAT(col1) INTO objs1 FROM T;
SET objs =CONCAT('CREATE TABLE Title AS SELECT ',objs1);
SET @sql_txt = objs;
PREPARE stmt FROM @sql_txt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
call INTERBANKBONDQUOTE_TSP;
存储过程B:这样不管字段多少,只要数据库支持就可以创建成功,
但不如第一个方法简洁。
DROP PROCEDURE IF EXISTS INTERBANKBONDQUOTE_TSP_Title;
drop table if EXISTS Title;
CREATE PROCEDURE INTERBANKBONDQUOTE_TSP_Title()
proc:begin
DECLARE add_sql LONGTEXT;
DECLARE insert_sql LONGTEXT;
DECLARE nhh_sql varchar(200);
DECLARE column_name varchar(100);
DECLARE column_value varchar(100);
DECLARE mycount int;
DECLARE len int;
DECLARE strlen int;
DECLARE cursor_Title CURSOR for select col1 from INTERBANKBONDQUOTE_T;
create table Title(mid int);
insert into Title values (100);
select count(col1) into @mycount from INTERBANKBONDQUOTE_T;
OPEN cursor_Title;
REPEAT
FETCH cursor_Title INTO nhh_sql;
begin
set @mycount=@mycount-1;
set @strlen=CHARACTER_LENGTH(nhh_sql);
set @len=INSTR(nhh_sql,' ');
set @column_name=RIGHT(nhh_sql,@strlen-@len);
set @column_value=LEFT(nhh_sql,@len);
set @add_sql=CONCAT('ALTER table Title add COLUMN ',@column_name,' varchar(100)');
set @insert_sql=CONCAT('update Title set ',@column_name,'=',@column_value,' where mid=100');
PREPARE stmt1 FROM @add_sql;
EXECUTE stmt1;
PREPARE stmt2 FROM @insert_sql;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt1;
DEALLOCATE PREPARE stmt2;
end;
until @mycount<=0
END REPEAT;
CLOSE cursor_Title;
ALTER table Title drop column mid;
end proc;
call INTERBANKBONDQUOTE_TSP_Title;
篇5:创建动态MSSQL数据库表
复制代码代码如下:
'btnCreateOthers
'
Me.btnCreateOthers.Location=NewSystem.Drawing.Point(17,43)
Me.btnCreateOthers.Name=“btnCreateOthers”
Me.btnCreateOthers.Size=NewSystem.Drawing.Size(104,23)
Me.btnCreateOthers.TabIndex=5
Me.btnCreateOthers.Text=“创建规则和索引”
feedom.net
'
'btnDropTable
'
Me.btnDropTable.Location=NewSystem.Drawing.Point(138,43)
Me.btnDropTable.Name=“btnDropTable”
Me.btnDropTable.TabIndex=6
Me.btnDropTable.Text=“删除表”
'
'btnViewData
'
Me.btnViewData.Location=NewSystem.Drawing.Point(351,43)
Me.btnViewData.Name=“btnViewData”
Me.btnViewData.TabIndex=7
Me.btnViewData.Text=“查看数据”
'
'btnViewSP
'
Me.btnViewSP.Location=NewSystem.Drawing.Point(230,43)
feedom.net
Me.btnViewSP.Name=“btnViewSP”
Me.btnViewSP.Size=NewSystem.Drawing.Size(104,23)
Me.btnViewSP.TabIndex=8
Me.btnViewSP.Text=“查看存储过程”
'
'btnViewView
'
Me.btnViewView.Location=NewSystem.Drawing.Point(443,43)
Me.btnViewView.Name=“btnViewView”
Me.btnViewView.TabIndex=9
Me.btnViewView.Text=“查看视图”
'
'DataGrid1
'
Me.DataGrid1.DataMember=“”
Me.DataGrid1.HeaderForeColor=System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location=NewSystem.Drawing.Point(20,76)54com.cn
Me.DataGrid1.Name=“DataGrid1”
Me.DataGrid1.Size=NewSystem.Drawing.Size(500,183)
Me.DataGrid1.TabIndex=10
'
'Form1
'
Me.AutoScaleBaseSize=NewSystem.Drawing.Size(5,13)
Me.ClientSize=NewSystem.Drawing.Size(538,281)
Me.Controls.AddRange(NewSystem.Windows.Forms.Control(){Me.DataGrid1,Me.btnViewView,_
Me.btnViewSP,Me.btnViewData,Me.btnDropTable,Me.btnCreateOthers,Me.btnAlterTable,_
复制代码代码如下:
Me.CreateViewBtn,Me.CreateSPBtn,Me.CreateTableBtn,Me.CreateDBBtn})
Me.Name=“Form1”
Me.Text=“动态创建SQLServer数据库、表、存储过程等架构信息”
CType(Me.DataGrid1,System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
EndSub
#EndRegion
'创建数据库
PrivateSubCreateDBBtn_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)_
HandlesCreateDBBtn.Click
conn=NewSqlConnection(ConnectionString)
'打开连接
Ifconn.StateConnectionState.OpenThen
conn.Open()
EndIf
'MyDataBase为数据库名称
DimsqlAsString=“CREATEDATABASEMyDataBaseONPRIMARY(Name=MyDataBase_data,filename=”+_
54com.cn
“'D:\\MyDataBase.mdf',size=3,”+“maxsize=5,filegrowth=10%)logon”+“(name=MyDataBase_log,”+_
“filename='D:\\MyDataBase.ldf',size=3,”+“maxsize=20,filegrowth=1)”
cmd=NewSqlCommand(sql,conn)
Try
cmd.ExecuteNonQuery()
CatchaeAsSqlException
MessageBox.Show(ae.Message.ToString())
EndTry
EndSub
'创建表
PrivateSubCreateTableBtn_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)_
HandlesCreateTableBtn.Click
conn=NewSqlConnection(ConnectionString)
'打开连接
Ifconn.State=ConnectionState.OpenThen
conn.Close()
EndIf
ConnectionString=“DataSource=.;InitialCatalog=MyDataBase;UserId=sa;Password=;”
conn.ConnectionString=ConnectionString
conn.Open()
sql=“CREATETABLEmyTable”+“(myIdINTEGERCONSTRAINTPKeyMyIdPRIMARYKEY,”+_
“myNameCHAR(50)NOTNull,myAddressCHAR(255),myValuesFLOAT)”
cmd=NewSqlCommand(sql,conn)
Try
cmd.ExecuteNonQuery()
'添加纪录
54com.cn
sql=“INSERTINTOmyTable(myId,myName,myAddress,myValues)”+_
“VALUES(1001,_'【孟宪会之精彩世界】之一','xml.sz.luohuedu.net/',100)”
cmd=NewSqlCommand(sql,conn)
cmd.ExecuteNonQuery()
sql=“INSERTINTOmyTable(myId,myName,myAddress,myValues)”+_
“VALUES(1002,'【孟宪会之精彩世界】之二','www.erp800.com/net_lover/',99)”
★ 知道一个值jjfda,如何查找它在那个表,那个字段数据库教程
动态关联表数据库教程(精选5篇)




