Home > Code > VB.NET > VB.NET SQL Example: How to send SMS in VB.NET using SQL database

VB.NET SQL Example: How to send SMS in VB.NET using SQL database

by Logan Turner   on Mar 13, 2015   Category: VB.NET   |  Views: 785    |  Points: 25   |  Starter 


I thought I share this code snippet because I find this way of SMS sending an easy solution. I believe that even beginners can implement this VB.NET SQL SMS application, but I think some extent of perfection in the SMS technology is required for better understanding. Okay, let’s jump right in the middle of the project that allows you to send and receive SMS through database tables by using your own VB.NET application.

Project information:

- Language: VB.NET
- Level: Intermediate
- IDE: Microsoft Visual Studio
- Database server: MS SQL, MS SQL Express, MySQL, Access, Oracle, etc. I used MySQL (http://www.mysql.com/).
- Special prerequisites: .NET Framework (http://www.microsoft.com/hu-hu/download/details.aspx?id=17851), SMS Gateway (in order to let your PC to send/receive SMS transmissions to/from a telecommunications network). I used Ozeki NG (www.ozekisms.com).

Project description:

First of all, you need to create the necessary database tables: one for the outgoing messages, and another one for the incoming messages. The snippet shows my MySQL commands, but it can be changed of course depending on the database server you use. In order to be able to use your database you need to connect it to your SMS gateway, then you can insert an SMS into the table of the outgoing messages by using an SQL INSERT command. To execute this command, you need to connect to the database by using the myConnection.Open() method. Thereafter insert the message into the database ( mySqlCommand.ExecuteNonQuery() ), then close the database connection ( myConnection.Close() ). That's all there is to it! It wasn't that black magic, was it?


’ MySQL commands for creating database tables

’ CREATE TABLE ozekimessagein (
’ id int IDENTITY (1,1),
’ sender varchar(30),
’ receiver varchar(30),
’ msg nvarchar(160),
’ senttime varchar(100),
’ receivedtime varchar(100),
’ operator varchar(30),
’ msgtype varchar(30),
’ reference varchar(30),
’);
’ CREATE TABLE ozekimessageout (
’ id int IDENTITY (1,1),
’ sender varchar(30),
’ receiver varchar(30),
’ msg nvarchar(160),
’ senttime varchar(100),
’ receivedtime varchar(100),
’ operator varchar(100),
’ msgtype varchar(30),
’ reference varchar(30),
’ status varchar(30),
’ errormsg varchar(250)
’);

’ VB.NET source code

Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1

Private Sub bSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bSend.Click
Try
Dim dbUsername As String = "ozekiuser"
Dim dbPassword As String = "ozekipass"
Dim database As String = "ozeki"

Dim myConnection As New SqlConnection("Server=.\SQLEXPRESS;User ID=" _
& dbUsername _
& ";password=" _
& dbPassword _
& ";Database=" _
& database _
& ";Persist Security Info=True")


Dim mySqlQuery As String = "INSERT INTO ozekimessageout (receiver,msg,status) " _
& "VALUES ('" & tbReceiver.Text & "', '" & tbMessage.Text & "', 'send');"

Dim mySqlCommand As New SqlCommand(mySqlQuery, myConnection)

myConnection.Open()

mySqlCommand.ExecuteNonQuery()

myConnection.Close()


Catch ex As Exception
MessageBox.Show(ex.Message)
End Try





End Sub
End Class



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts