string query = "select * from cfo_daily_trans_hist";
try
{
using (SqlConnection connection = new SqlConnection(
cnnString))
{
SqlCommand command = new SqlCommand(query);
command.Connection = connection;
connection.Open();
var result = command.ExecuteReader();
DataTable datatable = new DataTable();
datatable.Load(result);
connection.Close();
}
}
So the var result is created through the ExecuteReader(); and HasRows is true, and it shows the correct amount of fields. However, the DataTable that I create from it is empty.
What am I doing wrong? I'm 99% sure it's getting data, but I don't know how to find it through the SqlDataReader object to make sure.
Thanks.
Instead of a SqlDataReader, use a SqlDataAdapter.
SqlDataAdapter myAdapter = new SqlDataAdapter(command);
myAdapter.Fill(datatable);
With a SqlDataAdapter, you don't need to explicitly call SqlConnection.Open() and SqlConnection.Close(). It is handled in the Fill() method.
You can try to add "COALESCE" in your sql command statement ... if you have some NULL value in your query result, you will have problem with dataTable
Related
I am trying to get the name of the employee from the database and fill it in the textbox for the respective employee id.
I tried this code but nothing is happening on the page. It just reloads and the textbox (name) is left blank only.
SqlConnection con = new SqlConnection(#"Data Source=DESKTOP-0FUUV7B\SQLEXPRESS;Initial Catalog=EmployeeDetails;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select * from ProfessionalDetails where EmpId='"+EmployeeId.Text+"'", con);
SqlDataReader da = cmd.ExecuteReader();
while (da.Read())
{
Name.Text = da.GetValue(1).ToString();
}
con.Close();
Better solution is to execute the sql statement through Parameterized value.
The details of that process is given below:
using (SqlConnection con = new SqlConnection(live_connectionString))
{
using (SqlCommand cmd = new SqlCommand("Query", con))
{
con.Open();
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#EmpId", employeeId);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
var ds = new DataSet();
da.Fill(ds);
string? name = ds.Tables[0].Rows[1]["Variable name"].ToString();
Name.Text =name;
};
}
}
As mentioned above in comments, you have lot of issues.
you should use using with the connection to dispose of them.
You should use parameterized queries to avoid SQL injection.
Put your code in try catch so that you can easily identify the root cause of the issue.
Define the connection string in config file three than defining in the c# code.
You don’t need to select all the columns. And please avoid select * in the query, instead just write your column name, as you want to select only one column here.
You can use ExecuteScalar, it’s used when you are expecting single value.
And first make sure that textbox has the expected value when you are calling this query.
As noted, use paramters, and BETTER use STRONG typed paramters.
And no need to use a dataset, this is a single table - so use a datatable.
thus:
string strSQL =
#"select * from ProfessionalDetails where EmpId= #ID";
using (SqlConnection con = new SqlConnection(Properties.Settings.Default.TEST4))
{
using (SqlCommand cmd = new SqlCommand(strSQL, con))
{
con.Open();
cmd.Parameters.Add("#ID", SqlDbType.Int).Value = EmployeeID.Text;
DataTable rstData = new DataTable();
rstData.Load(cmd.ExecuteReader());
if (rstData.Rows.Count > 0)
Name.Text = rstData.Rows[0]["Name"].ToString();
}
}
I want to return a list of data from database, I pass params to a stored procedure, it then returns results to a list
List<DataDetail> FetchData(GenRequest Request)
{
List<DataDetail> details = new List<DataDetail>();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
// con.Open();
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand("sp_Gen", con);
con.Open();
cmd.Parameters.AddWithValue("#ForcedATMAmt", ForcedATMAmt);
cmd.Parameters.AddWithValue("#OthersValue", OthersValue);
con.Close();
cmd.ExecuteReader();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
DataDetailobj = new DataDetail();
obj.AdditionalPremium = dr["AdditionalPremium"].ToString();
obj.ATMLimit = dr["ATMLimit"].ToString();
details.Add(obj);
}
}
return details
}
I tried the approach in the code above but when I hover over dr, the dr.HasRows is false. any way I can return the data to a list without DataRows?
Generally speaking, when working with a database connection your order of operations is:
Open the database connection
Run your queries/commands
Close the database connection
In your case, it should go something like this:
con.Open()
SqlCommand cmd = new SqlCommand("sp_Gen", con);
// add parameters
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
// run your code to get values from da
con.Close()
As a side note, I don't believe you need to call cmd.ExecuteReader(); yourself, but that SqlDataAdapter.Fill() will execute the command for you.
This is a sample query:
declare #tempTable table
(
Id bigint
)
-- populating with temp Ids
select *
from TargetTable
where Id not in
(
select Id
from #tempTable
)
And this is C# code:
public DataTable Get(string sql)
{
var dataTable = new DataTable();
using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand())
{
connection.Open();
command.Connection = connection;
command.CommandText = sql;
var dataReader = command.ExecuteReader();
dataTable.Load(dataReader);
}
return dataTable;
}
Running this code throws exception, complaining that:
Incorrect syntax near the keyword 'declare'.
Incorrect syntax near
')'.
I know it's possible to use join instead of #tempTable, but is there a way to run this query?
A SqlDataAdapter object can be used to populate a DataTable as follows. Instead of calling SqlCommand.ExecuteReader(), the SqlDataAdapter.Fill() method is used. In this example the argument to the Fill() method is the DataTable that will be populated. While this approach will work with the query you posted with the table variable, I'd strongly recommend converting this to a stored procedure and filling the DataTable from this instead. Additionally, unless the amount of data that's being sent into the table variable is very small using a temp table will offer more functionality (more accurate statistics, better DML performance, ROLLBACK participation, etc.) than table variables and I'd suggest using a temp table instead as well.
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataTable dataTable = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand command = new SqlCommand(cmd, connection);
da.SelectCommand = command;
connection.Open();
da.Fill(dataTable);
}
Stored Procedure Call:
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataTable dataTable = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
//use SP name for command text
SqlCommand command = new SqlCommand("usp_ProcedureName", connection);
//stored procedure command type
command.CommandType = CommandType.StoredProcedure;
da.SelectCommand = command;
connection.Open();
da.Fill(dataTable);
}
You can create a Stored Procedure of your query then add this to your code:
command.CommandType = CommandType.StoredProcedure;
Then use exec command
command.CommandText = "exec procedureName"
If your query location is in database itself.
I have the following:
String sql = "SELECT * FROM Temp WHERE Temp.collection = '" + Program.collection + "'";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
Program.defaultCollection = (String)cmd.ExecuteScalar();
And I want to get the second column after executing the statement. I know it will return only one row with two columns
I have read online that I will have to read each row of the result, is there any other way?
ExecuteScalar gets the first column from the first row of the result set. If you need access to more than that you'll need to take a different approach. Like this:
DataTable dt = new DataTable();
SqlDataAdapater sda = new SqlDataAdapter(sql, conn);
sda.Fill(dt);
Program.defaultCollection = dt.Rows[0]["defaultCollection"];
Now, I realize that the field name may not be defaultCollection, but you can fill that in.
From the MSDN documentation for ExecuteScalar:
Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.
Now, as a final bit of advice, please wrap all ADO.NET objects in a using statement. Like this:
using (SqlConnection conn = new SqlConnection(connString))
using (SqlDataAdapter sda = new SqlDataAdapter(sql, conn))
{
DataTable dt = new DataTable();
sda.Fill(dt);
// do something with `dt`
}
this will ensure they are properly disposed.
And I want to get the second column after executing the statement
It is not possible with execute scalar.
is there any other way
You have 2 options here either to use SqlDataAdapter or SqlDataReader.
For you using DataReader is a recommended approach as you don't need offline data or do other worh
by using SqlDataAdapter
using (SqlConnection c = new SqlConnection(
youconnectionstring))
{
c.Open();
/
using (SqlDataAdapter a = new SqlDataAdapter(sql, c))
{
DataTable t = new DataTable();
a.Fill(t);
if(t.Rows.Count > 0)
{
string text = t.Rows[0]["yourColumn"].ToString();
}
}
}
by using DataREader
using (SqlConnection connection =
new SqlConnection(connectionString))
{
SqlCommand command =
new SqlCommand(sql, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
//read data here
string text = reader.GetString(1)
}
reader.Close();
}
SqlCommand.ExecuteScalar() can be used only when the result have just one row and one column.
If you need more than one column to be returned, you should use something like this:
String sql = "SELECT * FROM Temp WHERE Temp.collection = '" + Program.collection + "'";
SqlConnection conn = new SqlConnection(connString);
using(SqlCommand cmd = new SqlCommand(sql, conn))
{
using(SqlDataReader rdr = cmd.ExecuteReader())
{
if(rdr.Read())
{
Program.defaultCollection = (String)rdr["Column1"];
Program.someOtherVar = (String)rdr["Column2"];
}
}
rdr.Close();
}
That will be the fastest way.
You can use a DataReader and read only the first column like:
IDataReader cReader = cmd.ExecuteReader();
if(cReader.Read())
{
string cText = cReader.GetString(1); // Second Column
}
ExecuteScalar only returns one value. You have to make sure your query only returns that value.
String sql = "SELECT temp.defaultCollection FROM Temp WHERE Temp.collection = '" + Program.collection + "'";
On a side note, read on SqlParameter. You don't want to concatenate values like that, you'll have a problem when the collection property contains a quote.
Most of my queries are very short, 1-3 records total. This is the code I have now. I am wondering if there is a way to capture userid directly from DataAdapter without going through a table. Thanks!
SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strCon);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataTable t = new DataTable();
dataAdapter.Fill(t);
int userid = 0;
if (t.Rows.Count > 0)
{
DataRow dr = t.Rows[0];
userid = dr.Field<int>(0);
If you have only one return value, you can use ExecuteScalar and you have ExecuteReader to get multiple return values.
Here's the msdn sample:
private static void CreateCommand(string queryString,
string connectionString)
{
using (SqlConnection connection = new SqlConnection(
connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand(queryString, connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(String.Format("{0}", reader[0]));
}
}
}
Use a reader:
http://msdn.microsoft.com/en-US/library/system.data.sqlclient.sqldatareader(v=vs.80).aspx
Quicker and less resources 1 record or hundreds.