try
{
string connetionString = null;
SqlConnection connection;
SqlDataAdapter adapter;
SqlCommand command = new SqlCommand();
DataSet ds = new DataSet();
int i = 0;
connetionString = "Data Source=servername;Initial Catalog=database;User ID=sa;Password=yourpassword";
connection = new SqlConnection(connetionString);// connection
connection.Open();
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "StoredProcedureName"; // ชื่อ Procedure
adapter = new SqlDataAdapter(command);
adapter.Fill(ds); // execute เสร็จ เก็บใส่ Dataset
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
MessageBox.Show(ds.Tables[0].Rows[i][0].ToString()); //Loop DataSet ออกมา
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Code (VB.NET)
Try
Dim connetionString As String = "Data Source=servername;Initial Catalog=database;User ID=sa;Password=yourpassword"
Dim connection As New SqlConnection(connetionString)
Dim cmd As New SqlCommand
cmd.CommandText = "StoredProcedureName"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = connection
connection.Open()
Dim adapter As System.Data.SqlClient.SqlDataAdapter
Dim ds As New DataSet
adapter = New System.Data.SqlClient.SqlDataAdapter
adapter.SelectCommand = cmd
adapter.Fill(ds, "delivery")
connection.Close()
For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
Console.WriteLine(ds.Tables(0).Rows(i)(0).ToString())
Next
Catch ex As Exception
Console.Write(ex.Message)
End Try