Subscribe to Subscribers
Talk to Webmaster Tony John

Forums » .NET » .NET »

How to connect sql server with dataset


Posted Date: 15 Jun 2012      Posted By:: chellappan     Member Level: Silver    Member Rank: 1417     Points: 1   Responses: 6



I need code in vb.net
I want data from sqlserver with dataset




Responses

#675598    Author: Prasad kulkarni        Member Level: Diamond      Member Rank: 8     Date: 15/Jun/2012   Rating: 2 out of 52 out of 5     Points: 3

it's bit easy thing
you need a connection object to database and dataadapter to fetch records from database and load it to dataset.
check following code


Dim szQuery As String = "select * from emp where empid=10"

Dim objAdp As OleDbDataAdapter = New OleDbDataAdapter(szQuery, "Connectionstring")

Dim objDS As new DataSet
objAdp.Fill(objDS , "emp") //give table name here



this is how we fill dataset using dataadapter
hope it helps

Thanks
Koolprasd2003
[DotNetSpider MVM]



 
#675601    Author: Anil Kumar Pandey      Member Level: Platinum      Member Rank: 1     Date: 15/Jun/2012   Rating: 2 out of 52 out of 5     Points: 3

Here is a sample code to read data in data set


Dim con As SqlConnection = New SqlConnection(ConnStr)
Dim strQuery As String = "select Name from emp where Id>100"
Dim ada As SqlDataAdapter = New SqlDataAdapter(strQuery, con)
Dim lds As DataSet = New DataSet
ada.Fill(lds)
gvSales.DataSource = lds
gvSales.DataBind


Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM



 
#675604    Author: sudhajosyula      Member Level: Gold      Member Rank: 46     Date: 15/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

Try the below code


imports System
imports System.Data
imports System.Data.SqlClient

Public Class Form1
Dim sqlConn as new SqlConnection("Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password")
Dim da as SqlDataAdapter
Dim ds as new DataSet
Dim Sql as String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
if Not Page.IsPostBack then
Try
sqlConn.Open()
Sql = "select * from emptable"
da = New SqlDataAdapter(Sql,sqlConn )
da.Fill(ds)
sqlConn.Close()
DataGridView1.Data Source= ds.Tables(0)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
end if
End Sub
End Class






 
#675606    Author: Pawan Awasthi      Member Level: Diamond      Member Rank: 4     Date: 15/Jun/2012   Rating: 2 out of 52 out of 5     Points: 3

Hai Chellappan,

To create the connection, first you need to create the Connection object and provide the attributes like server name, database name, user id and password as the connection string.
As DataSet work as the disconnected architecture, so you can create the data adapter class object and pass the query from where you want to retrieve the data.
After that fill the dataset using
da.Fill(ds) method where da is the object of data adapter class and ds is the DataSet object.
Hope it will give you the brief idea about the connection and pulling the data to dataset from your database.
Hope it will be helpful to you.

Regards,
Pawan Awasthi(DNS MVM)
+91 8143683708 (pawansoftit@gmail.com)
Outstanding Contribution Award..NTT Data Inc



 
#675690    Author: Asheej T K        Member Level: Diamond      Member Rank: 2     Date: 15/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,

Check below simple code,

Dim SQLCon As New SqlClient.SqlSQLConection
SQLCon.SQLConectionString = "Data Source=<Database Server>;Initial Catalog=Data base name;Integrated Security=SSPI;"
SQLCon.Open()

Dim SQLCmd As New SqlClient.SqlCommand
SQLCmd.CommandText = "SELECT name,address,city FROM employee"
SQLCmd.CommandType = CommandType.Text
SQLCmd.SQLConection = SQLCon

Dim dt As New DataTable
dt.Load(SQLCmd.ExecuteReader)

DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = dt

SQLCmd.Dispose()
SQLCon.Dispose()



Regards,
Asheej T K
Microsoft MVP[ASP.NET/IIS]
DotNetSpider MVM

Dotnet Galaxy



 
#675707    Author: Ravindran        Member Level: Diamond      Member Rank: 3     Date: 15/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

Try like this simple code


using System.Data;
using System.Data.SqlClient;
public partial class GridViewOperation : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection(@"Server= RAVI-PC\SQLEXPRESS;database=test1;uid=ravindran;pwd=srirangam;");
SqlCommand sqlcmd;
SqlDataAdapter da;
DataSet ds = new DataSet();

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridData();
}
}
void GridData()
{
sqlcmd = new SqlCommand("select * from emp", sqlcon);
sqlcon.Open();
da = new SqlDataAdapter(sqlcmd);
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}
}


Regards
N.Ravindran
Your Hard work never fails



 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : Open new window and perform some opration
Previous : Get Error In DataTable when includes the Imports.Microsoft.Interop.Excel
Return to Discussion Forum
Post New Message
Category:

Related Messages
Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2012 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.