I am trying to use the following query
Dim sqlQry As String = "SELECT * FROM tblTest where Name=#NM and Rank=#RN"
Then I fill my dataadapter by
Dim dAdt As New SqlDataAdapter(sqlQry, conStr)
But do not know where to put the parameters that I have set after where clause.
You can use the parameters like this:
Dim adapter As SqlDataAdapter = New SqlDataAdapter()
' Create the SelectCommand.
Dim command As SqlCommand = New SqlCommand("SELECT * FROM tblTest where Name=#NM and Rank=#RN", connection)
' Add the parameters for the SelectCommand.
command.Parameters.Add("#NM", SqlDbType.NVarChar, 15)
command.Parameters.Add("#RN", SqlDbType.NVarChar, 15)
adapter.SelectCommand = command
Check this MSDN Document
Create a parameter
SqlParameter param = new SqlParameter();
param.ParameterName = "#RN";
param.Value = inputCity;
Then add the param to your Sql Command.
SqlCommand cmd = new SqlCommand();
cmd.Parameters.Add(param);
If I understand what you're asking, you need to create an instance of an SqlCommand and use your sqlQry with that. Then use SqlCommand.Parameters.Add() or SqlCommand.Parameters.AddWithValue() to add your parameters. Initialize your SqlDataAdapter with the SqlCommand instead of the String you've created.
Using connection As New SqlConnection(conStr)
Dim command As New SqlCommand(sqlQry, connection)
command.Parameters.Add("#NM", SqlDbType.NVarChar, 100).Value="Your Value"
command.Parameters.AddWithValue("#RN", "Your Value")
Dim adapter As New SqlDataAdapter(command)
adapter.Fill(dataSet)
Return dataSet
End Using
Related
I am new to the vb and I want to create project where if I click this button, form 2 will show and the information from it will change depends on what room button I click. i know how to connect a database but I am so clueless on how to make it that way, please help me. Thank you everyone
Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\arima\Documents\Consumer & rooms Database.mdb")
Yup, Stuck
You also need an OleDbCommand associated with the connection. The OleDbCommand.CommandText property is where your SQL statements go. Then you can .Open() the connection, so the command object can execute the SQL statements.
Here's the general idea:
Using con As New OleDbConnection("connection string here"), _
cmd As New OleDbCommand("SELECT * FROM MyTable WHERE MyColumn = ?", con)
cmd.Parameters.Add("?", OleDbType.VarWChar, 25).Value = "SomeValue"
con.Open()
Using rdr As OleDbDataReader = cmd.ExecuteReader()
While rdr.Read()
' Do something with each record
End While
End Using
End Using
Alternatively, you can use an OleDbDataAdapter to fill a DataTable or DataSet:
Dim dt As New DataTable
Using con As New OleDbConnection("connection string here"), _
da As New OleDbDataAdapter("SELECT * FROM MyTable WHERE MyColumn = ?", con)
da.SelectCommand.Parameters.Add("?", OleDbType.VarWChar, 25).Value = "SomeValue"
da.Fill(dt)
End Using
Sorry for my noob question, But how can I add values in #id in a select command?
I'm using vb.net and MySQL
here is calling all the 'time' column, but I want to add a where in a query.
("Select time from dtr where id = #id") where will I put the parameters.addwithvalues command?
con = New MySqlConnection
con.ConnectionString = "server = localhost; userid = root; password=; database=mjb_payroll; SslMode=none"
Dim query As String = "Select time from dtr"
Dim cmd As New MySqlCommand(query, con)
Dim adpt As New MySqlDataAdapter(cmd)
Dim tbl As New DataTable()
adpt.Fill(tbl)
DataGridView1.DataSource = tbl
If your concern is just on adding parameters.addwithvalue you can add it after declaring your MySqlCommand:
Dim query As String = "Select time from dtr where id = #id"
Dim cmd As New MySqlCommand(query, con)
cmd.Parameters.AddWithValue("#id", id)
Dim adpt As New MySqlDataAdapter(cmd)
Dim tbl As New DataTable()
I know that that ConfigurationManager.AppSettings("ADOConnectionString") gives the right location for my connection, and family_id is the number 6 (at family_id=6 I know I have the right data in my Product table) so why won't this run past execute reader?
Dim sqlConnection1 As New SqlConnection(ConfigurationManager.AppSettings("ADOConnectionString"))
Dim cmd As New SqlCommand
Dim reader As SqlDataReader
cmd.CommandText = "SELECT * FROM [TID].[dbo].[Product] WHERE family_id=#family_id"
cmd.Parameters.AddWithValue("#family_id", family_id)
cmd.CommandType = CommandType.Text
sqlConnection1.Open()
reader = cmd.ExecuteReader()
You haven't attached the connection to your command. One way to do that is:
cmd.Connection = sqlConnection1
Another is to do it as part of the SqlCommand constructor:
Dim sql As String
Dim cmd As SqlCommand
Dim reader As SqlDataReader
sql = "SELECT * FROM [TID].[dbo].[Product] WHERE family_id=#family_id"
cmd = new SqlCommand(sql, sqlConnection1)
I am trying to improve performance of an application, I have a case where a common SPROC is being used but is it necessary to fill a DataTable just to set 2 variable values?
Is there anything more efficient?
Dim Conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("DB").ConnectionString)
Dim CmdUsers As SqlCommand = New SqlCommand("uspGetUsers", Conn)
CmdUsers.CommandType = CommandType.StoredProcedure
CmdUsers.Parameters.Add(New SqlParameter("#UserName", Session("UserID")))
Dim da As SqlDataAdapter = New SqlDataAdapter
Dim dtUserInfo As DataTable = New DataTable
da = New SqlDataAdapter(CmdUsers)
da.Fill(dtUserInfo)
isParent = dtUserInfo.Rows(0)("IsAdmin")
UserVal = dtUserInfo.Rows(0)("UserVal")
A SqlDataReader is the fastest way to read data from a query. Try the following:
Using conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("DB").ConnectionString)
conn.Open()
Using cmd As New SqlCommand("uspGetUsers", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("#UserName", Session("UserID"))
Using reader As SqlDataReader = cmd.ExecuteReader()
While reader.Read()
isParent = reader("IsAdmin")
UserVal = reader("UserVal")
End While
End Using
End Using
End Using
You may need to parse the data to the correct types.
Also, note the use of Using to automatically dispose of the connection, command and reader objects: http://msdn.microsoft.com/en-GB/library/htd05whh.aspx
Can I use two command object with one open connection in one procedure at VB.NET?
Yes you can. As long as you don't close the connection between commands, it'll work fine.
This is a C# example, but I'm sure you can work it out:
using (SqlConnection cn = new SqlConnection("Connection String"))
{
SqlCommand cmd1 = new SqlCommand("Command1", cn);
cmd1.CommandType = CommandType.StoredProcedure;
SqlCommand cmd2 = new SqlCommand("Command2", cn);
cmd2.CommandType = CommandType.StoredProcedure;
cn.Open();
// Execute cmd1
// Execure cmd2
}
Example; kinda pseudo but you should get the concept.
dim cnn as connection
dim cmd as command
dim cmd2 as command
dim str1 as string
dim str2 as string
cnn.open
cmd.connection = cnn
cmd.command = str1
cmd.execute
cmd2.connection = cnn
cmd2.command = str2
cmd2.execute
cnn.close