制作插入功能 [英] Make An insert Function

查看:47
本文介绍了制作插入功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用函数在Sql Server中插入数据



使用VB.Net

------------ -------------

i想要插入功能或方法或类别



与谁一起工作任何表格和任何栏目

没有任何变化

我应该为这些代码做什么

---------- ------------------------

i只需添加

  dim  as   string  
table = scott





  class  .insert( 1  2  3  4  5  6 ,< span class =code-digit> 7 , 8 



  class  .i插入( 8  8  8  8  8  8  8  8  8  8  ,,  8  8 

意味着在功能上没有任何改变

--------------------------------- ---------

只需给表名和值插入....



虚假格式删除,添加了代码块 - OriginalGriff [/ edit]

解决方案

很可能,你不能这样做,或者没有很多工作来创建一个通用例程。

表名没问题:只需将其插入SQL INSERT语句:

  Dim  sqlCommand  as   String  =  插入& tableName&   ...  

问题来自实际的表数据本身。

除非你确切知道表中的内容,否则插入很可能会失败:因为你试图对此进行通用,你不能列出你要去的列要插入,所以你不知道你要插入多少行数据。由于SQL INSERT语句是基于行的,这就成了一个真正的问题。



此外,如果您的任何列都是标识字段,则无法为它们提供值,因此您必须在INSERT语句中明确命名您希望加载的字段(并且这被认为是良好的做法)所以无论如何)。



说实话,我不会这样做 - 只需使用一个知道数据库结构的DL并以这种方式保存数据


Insert Data in Sql Server using function

using VB.Net
-------------------------
i want to make an insert fucntion or method or class

who work with any table and any column
with out any changes
what should i do for these code
----------------------------------
i just add

dim table as string
table = scott


and

class.insert(1,2,3,4,5,6,7,8)

or

class.insert(8,8,8,8,8,8,8,8,8,8,,8,8)

means no change any things in function
------------------------------------------
just give table name and values to insert....

[edit]Spurious formatting removed, code blocks added - OriginalGriff[/edit]

解决方案

In all probability, you can''t do that, or not without a lot of work to create a generic routine.
The table name is no problem: just insert it into your SQL INSERT statement:

Dim sqlCommand as String = "INSERT INTO " & tableName & "...

The problem comes with the actual table data itself.
Unless you know exactly what the table holds, there is a very good chance that the insert will fail: because you are trying to be generic about this, you can''t list the columns that you are going to insert, so you don''t know how many rows worth of data you are trying to insert. Since the SQL INSERT statement is row based, this becomes a real problem.

In addition, if any of your columns are Identity fields, you cannot supply a value for them so you have to explicitly name the fields you do wish to load in the INSERT statement (and it''s considered good practice to do so anyway).

To be honest, I wouldn''t do it - just use a DL that "knows" about the database structure and save data that way instead.


这篇关于制作插入功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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