VB.NET SQL Example: How to send SMS in VB.NET using SQL database
Join the DZone community and get the full member experience.Join For Free
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. Here’s a taste of the program:
- Language: VB.NET
- Level: Intermediate
Microsoft Visual Studio
- Database server: MS SQL, MS SQL Express, MySQL,
Access, Oracle, etc. I used MySQL.
prerequisites: .NET Framework,
SMS Gateway (in order to let your PC to send/receive SMS transmissions to/from
a telecommunications network). I used Ozeki NG.
First of all, you need to create the necessary database tables: one for the outgoing messages, and another one for the incoming messages. The following snippet shows my MySQL commands, but it can be changed of course depending on the database server you use:
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) );
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?
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
Opinions expressed by DZone contributors are their own.