第1部分:应用场景
我们在运用visual basic(vb.net)开发应用程序的过程中,
数据流转可谓是必然,
但是采用好的方法进行数据流转,
常常可以达到事半功倍的效果,
这次我就向大家介绍这一个方法。
第2部分:解决方法
首先构建一个visual basic(vb.net)获取相同字段的公共过程,
sql语言可以参考我的另一篇文章《sql server中如何获取两个数据表中相同的字段》,
Private Function GetColName(ByVal sTableName As String, ByVal dTableName As String) As String Try Dim Sql As String Dim Dst As New DataSet Sql = "select a.name from ( " + vbCrLf + _ " select b.name from sysobjects a left join syscolumns b on a.id = b.id where a.name = '" + 表1名称 + "' " + vbCrLf + _ " ) a " + vbCrLf + _ " left join ( " + vbCrLf + _ " select b.name from sysobjects a left join syscolumns b on a.id = b.id where a.name = '" + 表2名称 + "' " + vbCrLf + _ " ) b on a.name = b.name " + vbCrLf + _ " where ISNULL(b.name,'') <> '' " Dst = Exec(Sql, ExecType.DataSet) rem 这里是构建字符串 Dim sstr As String = "" For Each row In Dst.Tables(0).Rows sstr += "[" + row(0) + "]," Next Return sstr.Trim(",") Catch ex As Exception Throw ex End Try End Function
然后,调用这个过程获得相同字段的字符串,
同时可以通过replace进行修改字符串对应内容,
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try rem 调用过程获取相同字段 Dim tblPipesStockColumn As String = GetColName("tblPipesStock", "tblDoOrderSplitWeight") rem 给字段添加别名 Dim tblProducts_all_OutStockDetail_AColumn As String = "a." + Replace(tblPipesStockColumn, ",", ",a.") rem 对部分字段做特殊处理 tblProducts_all_OutStockDetail_AColumn = Replace(tblProducts_all_OutStockDetail_AColumn, "a.[Pcs]", "0 Pcs") tblProducts_all_OutStockDetail_AColumn = Replace(tblProducts_all_OutStockDetail_AColumn, "a.[Weight]", "0 Weight") rem 使用插入语句 Sql = "Insert into tblPipesStock(" + tblPipesStockColumn + ")" Sql += " Select " + tblProducts_all_OutStockDetail_AColumn + " " + vbCrLf + _ " from tblDoOrderSplitWeight a " + vbCrLf + _ " left join tblPipesStock b on a.PcsNo = b.PcsNo" + vbCrLf + _ " where a.StripOrderNo = '123456' " Exec(Sql, ExecType.NonQuery) Catch ex As Exception MsgBox(ex.Message.ToString) End Try End Sub
第3部分:结论
通过这样一个方法,极大的减少visual basic(vb.net)的代码量和出错率,
而且如果是直接用字段插入,一般非常长,容易看花眼!
© 版权声明
THE END
暂无评论内容