Vb.net和Sqlserver [英] Vb.net and Sqlserver

查看:144
本文介绍了Vb.net和Sqlserver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查记录是否存在?而且当记录不存在时我也可以插入记录


How can check the record is exists or not? And also i can insert the record when its not existing


Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
            con = New SqlConnection(Constr)
            con.Open()
            Dim cmdstr1 As String = "Select * from Mcdaily where Mcno=''" & McnoCombo.Text & "''and Ftime=" & DGVlogentry.Item(2, i).Value & ""
            dt = DataManager.ExecuteQuery(cmdstr1, Constr)
            If (dt.Rows.Count > 0) Then
                MessageBox.Show("Already Exists")
            Else
                 Insertcmd()
             End if 
End sub

推荐答案

用于检查的代码已经存在.
那么您的问题在哪里?
You code for checking is already there.
So where is your problem?


此代码正确,您在哪里打了?

我的一点建议.
您可以使用选择计数(*)......."而不是选择* .......".
然后,您可以使用ExecuteScalar()方法,该方法将直接返回行数,并且不需要使用数据表.
只是一个建议. :)

顺便说一句,您面临什么问题?
This code is correct, where are you struck?

A little suggestion from me.
You can use "select count(*)......." rather than "select *.......".
Then you can use ExecuteScalar() method which will directly return count of rows and you won''t need a datatable to work with.
Just a suggestion. :)

BTW, what problem you are facing?


还使用参数而不是将字符串连接到SQL文本.有关更多信息: SqlParameter [
Also use parameters instead of concatenating string to the SQL text. For more info: SqlParameter[^]


这篇关于Vb.net和Sqlserver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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