16,555
社区成员
发帖
与我相关
我的任务
分享
Public Sub insertdata(ByVal s1 As String, ByVal s2 As String, ByVal s3 As String)
Dim sql As String
Dim con As New SqlClient.SqlConnection("Data Source=10.168.1.5;Initial Catalog=data;User ID=sa;password=sa;Integrated Security=False")
con.Open()
Dim sqlCommand As SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(String.Format("select Count(*) from newtable where a= '{0}'", s1), con)
Dim rowsAffected As Integer = sqlCommand.ExecuteScalar()
If sqlCommand.ExecuteScalar() > 0 Then
MessageBox.Show(s1 & " 数据已经存在")
Else
sql = "insert into newtable(a,b,c) values('" + s1 + "','" + s2 + "','" + s3 + "')"
sqlCommand.CommandText = sql
sqlCommand.ExecuteNonQuery()
MessageBox.Show(s1 & " 成功添加!")
End If
sqlCommand.Dispose()
sqlCommand.Clone()
End Sub
connectionstring = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source=" & My.Application.Info.DirectoryPath & "\MyCalculateDB.accdb;" '路径为G:\VBprogremm\Calculate\Calculate\bin\Debug
Dim querystring As String
Try
If CheckBox1.Checked = False Then
querystring = "insert into projectdata(province,city,returnperiod,groundtype,antiseismic,us1,us2,A,zh,wk1,wk2,earthquakeeffect,projectname) values ( '" & ComboBox1.Text & "','" & ComboBox2.Text & "','" & returnperiod & "','" & groundtype & "','" & ComboBox3.Text & "','" & us1 & "','" & us2 & "','" & 0 & "','" & zh & "','" & wk1 & "','" & wk2 & "','" & TextBox8.Text & "','" & TextBox3.Text & "' )" 'select projectname from projectdata where not exists(select * from projectdata where projectname = '" & TextBox3.Text & " ')"
Else
querystring = "insert into projectdata(province,city,returnperiod,groundtype,antiseismic,us1,us2,A,zh,wk1,wk2,earthquakeeffect,projectname) values ( '" & ComboBox1.Text & "','" & ComboBox2.Text & "','" & returnperiod & "','" & groundtype & "','" & ComboBox3.Text & "','" & us1 & "','" & us2 & "','" & A & "','" & zh & "','" & wk1 & "','" & wk2 & "','" & TextBox8.Text & "','" & TextBox3.Text & "' )" 'select projectname from projectdata where not exists(select * from projectdata where projectname = '" & TextBox3.Text & " ')"
End If
Dim querystring_select As String
'查询是否已存在记录
querystring_select = "select projectname from projectdata where not exists(select * from projectdata where projectname = '" & TextBox3.Text & " ')"
Using conn As New OleDb.OleDbConnection(connectionstring)
conn.Open() '打开连接
' Dim cmd As New OleDb.OleDbCommand(querystring, conn)
'cmd.ExecuteNonQuery()
Dim cmd As New OleDb.OleDbCommand(conn)
cmd.CommandText = querystring_select
Dim selectObj As Object
selectObj = cmd.ExexuteScalar()
If DbNull.Value.Equals(selectObj) Then
'如果不存在记录就插入数据
cmd.CommandText = querystring
cmd.ExecuteNonQuery()
End If
End Using
MsgBox("数据保存成功!")
Catch ex As Exception
MsgBox(ex.Message)
End Try