【代码醒目】求VB做SQL-SERVER应用的代码,多多宜善【代码醒目】

thirdapple 2004-01-18 11:51:24
谢谢了先,200分不成敬意,我从来不搞这个不知道,还请各位大虾帮忙!
...全文
72 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
sysroger 2004-01-26
  • 打赏
  • 举报
回复
www.dapha.net
chenyu5188 2004-01-26
  • 打赏
  • 举报
回复
www.dapha.net
www.vbgood.com上有大量的例程下载.

1.用来清空日志,工作中经常用到:
USE [Database Name]
GO
BACKUP LOG [Database Name]WITH NO_LOG
GO
DBCC SHRINKDATABASE([Database Name],50)
GO

2.图片导入数据库:
Private Sub Command3_Click()
Dim conn As New ADODB.Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\1.mdb;Persist Security Info=False"
conn.Execute "create table a (b longbinary)"
End Sub

Private Sub Command4_Click()
Set b = New ADODB.Recordset
Set c = New ADODB.Stream


c.Mode = adModeReadWrite

c.Type = adTypeBinary
c.Open
c.LoadFromFile "c:\1.bmp"

b.Open "select * from a", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\1.mdb;Persist Security Info=False", adOpenDynamic, adLockOptimistic
b.AddNew

b.Fields.Item(0).Value = c.Read()


b.Update

b.Close
Set b = New ADODB.Recordset
b.Open "select * from a", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\1.mdb;Persist Security Info=False", adOpenKeyset, adLockOptimistic
MsgBox b.RecordCount

b.MoveLast

c.Write (b.Fields.Item(0).Value)

c.SaveToFile "c:\aa.bmp", adSaveCreateOverWrite

Picture1.Picture = LoadPicture("c:\aa.bmp")
End Sub


简易无忌 2004-01-26
  • 打赏
  • 举报
回复
该顶一下了
lk_cool 2004-01-19
  • 打赏
  • 举报
回复
这麽多高手
我都不敢发言了
online 2004-01-19
  • 打赏
  • 举报
回复
http://www.dapha.net/down/show.asp?classid=1&Nclassid=111
zjcxc 2004-01-19
  • 打赏
  • 举报
回复
强烈同意楼上.

楼上也来转啦.
txlicenhe 2004-01-19
  • 打赏
  • 举报
回复
本版的每一个贴子基本上都是。

建议看看本版的FAQ
  • 打赏
  • 举报
回复
--列举网络中其他SQL SERVER存在空帐户'sa'的示例
-- Create temp table to store enumerated servers

SET NOCOUNT ON

CREATE TABLE #temp (shelldump varchar(255))

INSERT #temp EXEC xp_cmdshell 'osql -L'

DECLARE @current_server varchar(255), @conn_string varchar(255)

DECLARE sql_cursor CURSOR FOR SELECT * FROM #temp

OPEN sql_cursor FETCH NEXT FROM sql_cursor INTO @current_server

-- Loop through potential targets and check for null sa accounts

-- If target is vulnerable, version information will be displayed

WHILE @@FETCH_STATUS = 0

BEGIN

If @current_server <> 'Servers:'

BEGIN

SELECT @current_server = rtrim(ltrim(@current_server))

SELECT @conn_string = 'exec xp_cmdshell ''osql -S' + @current_server + ' -Usa -P -Q "select @@version"'''

PRINT 'Attempting connection to server: ' + @current_server

EXECUTE (@conn_string)

PRINT '====================================================================='

END

FETCH NEXT FROM sql_cursor INTO @current_server

END

--Clean up

CLOSE sql_cursor

DEALLOCATE sql_cursor

DROP TABLE #TEMP
  • 打赏
  • 举报
回复
代码也有,我也来点

--根据表中数据生成insert语句的存储过程

--建立存储过程,执行 proc_insert 表名




CREATE proc proc_insert (@tablename varchar(256))

as

begin

set nocount on

declare @sqlstr varchar(4000)

declare @sqlstr1 varchar(4000)

declare @sqlstr2 varchar(4000)

select @sqlstr='select ''insert '+@tablename

select @sqlstr1=''

select @sqlstr2=' ('

select @sqlstr1= ' values ( ''+'

select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from (select case

-- when a.xtype =173 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'

when a.xtype =104 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(1),'+a.name +')'+' end'

when a.xtype =175 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'

when a.xtype =61 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'

when a.xtype =106 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'

when a.xtype =62 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'

when a.xtype =56 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(11),'+a.name +')'+' end'

when a.xtype =60 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'

when a.xtype =239 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'

when a.xtype =108 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'

when a.xtype =231 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'

when a.xtype =59 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'

when a.xtype =58 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'

when a.xtype =52 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(12),'+a.name +')'+' end'

when a.xtype =122 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'

when a.xtype =48 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end'

-- when a.xtype =165 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'

when a.xtype =167 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'

else '''NULL'''

end as col,a.colid,a.name

from syscolumns a where a.id = object_id(@tablename) and a.xtype <>189 and a.xtype <>34 and a.xtype <>35 and a.xtype <>36

)t order by colid



select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename

-- print @sqlstr

exec( @sqlstr)

set nocount off

end

GO
  • 打赏
  • 举报
回复
什么意思?没太懂


我个人建议把各种商业逻辑、业务流程、规则写成存储过程或触发器中,就像封装一样,然后
在vb这端只是简单加以显示和处理,然后提供操作数据的接口就行了。
逻辑或流程复杂时可能还要用到事务吧。

望楼下的能指正或继续交流
zjcxc 2004-01-19
  • 打赏
  • 举报
回复
'来一个

'*************************************************************************
'**
'** 使用 ADODB.Stream 保存/读取文件到数据库
'** 引用 Microsoft ActiveX Data Objects 2.5 Library 及以上版本
'**
'** ----- 数据库连接字符串模板 ---------------------------------------
'** ACCESS数据库
'** iConcStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
'** ";Data Source=数据库名"
'**
'** SQL数据库
'** iConcStr = "Provider=SQLOLEDB.1;Persist Security Info=True;" & _
'** "User ID=用户名;Password=密码;Initial Catalog=数据库名;Data Source=SQL服务器名"
'**
'*************************************************************************
'
'保存文件到数据库中
Sub s_SaveFile()
Dim iStm As ADODB.Stream
Dim iRe As ADODB.Recordset
Dim iConcStr As String

'数据库连接字符串
iConcStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
";Data Source=F:\My Documents\客户资料1.mdb"

'读取文件到内容
Set iStm = New ADODB.Stream
With iStm
.Type = adTypeBinary '二进制模式
.Open
.LoadFromFile "c:\test.doc"
End With

'打开保存文件的表
Set iRe = New ADODB.Recordset
With iRe
.Open "表", iConc, adOpenKeyset, adLockOptimistic
.AddNew '新增一条记录
.Fields("保存文件内容的字段") = iStm.Read
.Update
End With

'完成后关闭对象
iRe.Close
iStm.Close
End Sub

'从数据库中读取数据,保存成文件
Sub s_ReadFile()
Dim iStm As ADODB.Stream
Dim iRe As ADODB.Recordset
Dim iConc As String

'数据库连接字符串
iConc = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
";Data Source=\\xz\c$\Inetpub\zj\zj\zj.mdb"

'打开表
Set iRe = New ADODB.Recordset
iRe.Open "tb_img", iConc, adOpenKeyset, adLockReadOnly
iRe.Filter = "id=64"

'保存到文件
Set iStm = New ADODB.Stream
With iStm
.Mode = adModeReadWrite
.Type = adTypeBinary
.Open
.Write iRe("img")
.SaveToFile "c:\test.doc"
End With

'关闭对象
iRe.Close
iStm.Close
End Sub
flc 2004-01-19
  • 打赏
  • 举报
回复
关注
lepeng 2004-01-19
  • 打赏
  • 举报
回复
http404错误
online 2004-01-19
  • 打赏
  • 举报
回复
看www.dapha.net里有大量的例子

1,217

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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