segunda-feira, 26 de novembro de 2012

PHP Connect to MySql


<?PHP

define ("DEBUG", true); //to authorize debugging

  function connectSRVBD(){
   
        $host="localhost"; // or IP // Server where database is located
       
        $user="DB_UserName"; //user with rights to connect to database
       
        $pass="DB_Password"; // user password
       
        $schema="DataBase"; // the database name
       
        $port=3306;  //Port by Default ;
       
        $db=mysqli_connect($host, $user, $pass, $schema, $port);
       
        $errorCode=mysqli_connect_errno(); // Mysql - Error Code
        $errorMessage=mysqli_connect_error(); // Mysql - Error Message

        if (DEBUG && $errorCode>0)
            echo "ERROR connectSRVBD : $errorCode : $errorMessage<br>";
                                     
        return $errorCode===0?$db:false;
    }
   
?>

 

quarta-feira, 17 de outubro de 2012

Add columns to DataTable



static void Main(string[] args)
{

DataTable dt = new DataTable();

// Add the column to the DataTable to create
DataColumn col1 = dt.Columns.Add();

// Configure the column
//-- integer with a default = 0
// that  does not allow nulls
col1.ColumnName = "1Column";
col1.DataType = typeof(int);
col1.DefaultValue = 0;
col1.Unique = true;
col1.AllowDBNull = false;

// Create  the column
DataColumn col2 = new DataColumn();

// Configure the column
//string with max length = 50
col2.ColumnName = "2Column";
col2.DataType = typeof(string);
col2.MaxLength = 50;


// Add column to the DataTable
dt.Columns.Add(col2);

// Add a column directly using an overload of the Add()
 // method of the DataTable.Columns collection -- the column
 // is a string with max length = 50
dt.Columns.Add("3Column", typeof(string)).MaxLength = 50;

// Add multiple existing columns to the DataTable
DataColumn col4 = new DataColumn("4Column");

// ... configure column 5
DataColumn col5 = new DataColumn("5Column", typeof(int));

// Add columns 4 and 5 to the DataTable
dt.Columns.AddRange(new DataColumn[] { col4, col5 });

// Show the columns in the DataTable to the console
Console.WriteLine("DataTable has {0} DataColumns named:",dt.Columns.Count);

foreach (DataColumn col in dt.Columns)

Console.WriteLine("\t{0}", col.ColumnName);
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}










Table Mapping in DOT.NET



static void Main(string[] args)
{

// Configure Sql Provider - to connect to server or database
  string sqlConnectString = @"Data Source=(local)\sqlexpress;" +
  "Integrated security=SSPI; Initial Catalog=DataBase;";

// Creating a table Cliente in a DataBase (Sql, Access...)

 // Query to get desired data
  String sqlSelect = "SELECT TOP 5 IDCliente,Apelido,PrimNome " +
  "From Cliente";

 //Get data from sql to adapter
  SqlDataAdapter da = new SqlDataAdapter(sqlSelect, sqlConnectString);

//Mappeding the table
  DataTableMapping dtm =da.TableMappings.Add("Table", "mappedCliente");

// Mapping the column
  dtm.ColumnMappings.Add("IDCliente", "mappedIDCliente");
  dtm.ColumnMappings.Add("Apelido", "mappedApelido"); //...

   dtm.ColumnMappings.Add("PrimNome", "mappedPrimNome"); //...

 // (DataSet = DataBase Virtual)
  DataSet ds = new DataSet();

 // dataset get data from adapter 
   da.Fill(ds);
  Console.WriteLine("DataTable name = {0}",ds.Tables[0].TableName);

// show the mapped columns
 foreach (DataColumn col in ds.Tables["mappedCliente"].Columns)

  {
   Console.WriteLine("\tDataColumn {0} name = {1}",col.Ordinal, col.ColumnName);
  }

 Console.WriteLine();

// A break to view the mapped columns - Enter to continue
 Console.ReadKey(); 

// show data in rows
  foreach (DataRow row in ds.Tables[ "mappedCliente"].Rows)

  {
  Console.WriteLine("IDCliente = {0}, Apelido = {1}, PrimNome = {2}",
      row["mappedIDCliente"], row["mappedApelido"],row["mappedPrimNome"]);

  }

  Console.WriteLine("\nEnd DataTableMapping - Press any Key to           continue");
  Console.ReadKey(); 

}


sexta-feira, 7 de setembro de 2012

Sending SMS using VOIP Web Service


An easy and simple way  to use VOIP services available on the web to send cheap SMS (Messages).

SMS - Sending Page
Source Code:

Imports System.Net
Imports System.IO 

Public Class Form1

Dim LS As New LSettings 'Classe of SMS Server Config

Dim SMSserver, User, Pass, Remetente, Destinatario, SMStext As String

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

If txt_destinatario.Text = String.Empty Or txt_msg.Text = String.Empty Then

MessageBox.Show("Number or Message can not be Empty!")

Else
Cursor = Cursors.WaitCursor
' Create a new web client to send the requestDim client As WebClient = New WebClient()SMSserver = LS.server
User = LS.user
Pass = LS.pass
Remetente = LS.remetente
Destinatario =
"Country Code EX: 00351 " & txt_destinatario.Text & ""SMStext = "" & txt_msg.Text & ""
' Set the URL to send the request to, please note' you can use either HTTP or HTTPS

Dim url As String = _
"https://" _& SMSserver &
"username=" _& User &
"&password=" _& Pass &
"&from=" _& Remetente &
"&to=" _& Destinatario &
"&text=" _& SMStext &
""
Try
' Send the request and read the response
Dim stream As Stream = client.OpenRead(url)
Dim reader As StreamReader = New StreamReader(stream)
Dim response As String = reader.ReadToEnd()

' Close the data stream and HTTP connection
stream.Close()
reader.Close()
Catch ex As ExceptionMsgBox(ex.Message _
& vbNewLine _
&
"The message is in sending state")

End Try
Cursor = Cursors.Default
lblSMSinfo.Visible = True

End If

End Sub

Private Sub txt_destinatario_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txt_destinatario.MouseClick

lblSMSinfo.Visible = False

End Sub

Private Sub bt_limpar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_limpar.Click

txt_destinatario.Clear()
txt_msg.Clear()
lblSMSinfo.Visible = False

End Sub

Private Sub SettingsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SettingsToolStripMenuItem.Click

SMSSettings.Show()

End Sub

'Limit insert text only numbers

Private Sub txt_destinatario_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_destinatario.KeyPress

Dim KeyAscii As Short = CShort(Asc(e.KeyChar))
KeyAscii = CShort(LS.ValidarNumero(KeyAscii, "0123456789"))

If KeyAscii = 0 Then
e.Handled = True

End If

End Sub

End Class


SMS - Config. SMS Server Page
Source Code:

Public Class LSettings

Private _server As StringPrivate _user As StringPrivate _pass As StringPrivate _remetente As String 

Public Property server() As String
Get
Return _server
End Get

Set(ByVal value As String)
_server = value

End Set
End Property 

Public Property user() As StringGet
Return _user
End Get

Set(ByVal value As String)
_user = value

End Set
End Property

Public Property pass() As String
Get
Return _pass
End Get

Set(ByVal value As String)
_pass = value

End Set
End Property

Public Property remetente() As String
Get
Return _remetente
End Get

Set(ByVal value As String)
_remetente = value

End Set
End Property

Public Sub Settings(ByVal Dserver As String, ByVal Duser As String, _
                     ByVal Dpass As String, ByVal Dremetente As String)
server = Dserveruser = Duser
pass = Dpass
remetente = Dremetente
End Sub

Public Function ValidarNumero(ByVal Keyascii As Short, ByVal filtroDados As String) As Short'filtroDados = "0123456789,-+" Exemplo

If InStr(filtroDados, Chr(Keyascii)) = 0 Then
ValidarNumero = 0
ElseValidarNumero = Keyascii
End If

Select Case Keyascii
Case 8
ValidarNumero = Keyascii
Case 13
ValidarNumero = Keyascii
Case 32
ValidarNumero = Keyascii
End Select
End Function

End Class