sqldatareader

Trouble loading SQL Data Reader data into DataTable

筅森魡賤 提交于 2019-11-30 17:13:16
问题 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

Using SQLDataReader instead of recordset

假如想象 提交于 2019-11-30 07:06:10
I am new to this and had this question. Can I use SQLDataReader instead of a Recordset. I want to achieve the following result in an SQLDataReader. Dim dbConn As New ADODB.Connection Dim rs As New ADODB.Recordset Dim sqlstr As String = "SELECT Name,Status FROM table1 WHERE id=" + item_id.Value.ToString rs.Open(SQL, dbConn) While Not rs.EOF txtName.Text = rs.Fields.Item("Name").Value ddlstatus.SelectedIndex = 1 rs.MoveNext() End While rs.Close() rs = Nothing dbConn.Close() dbConn = Nothing Can I replace recordset with SQLDataReader and if I can can you please show me the changes in code? Its

SQL Server and SqlDataReader - Trillion Records - Memory

爱⌒轻易说出口 提交于 2019-11-30 05:07:36
问题 I've never tried this - so I don't know if I'd run into memory issues. But can a SqlDataReader read a trillion records? It's all streamed correct? I'm a little green to what the SQL/TDS protocol is doing under the covers. UPDATE Translate Trillion to mean very large number. I probably should have said something like 1 billion or 100 million. 回答1: Yes, that will stream... but I don't think you should actually try to do it. If you could read a million records per second (which sounds unlikely

SQLDataReader Row Count

你说的曾经没有我的故事 提交于 2019-11-30 01:20:32
问题 I am trying to get the number of rows that were returned by iterating the reader. But I always get 1 when I run this code? Did I screw up something in this? int count = 0; if (reader.HasRows) { while (reader.Read()) { count++; rep.DataSource = reader; rep.DataBind(); } } resultsnolabel.Text += " " + String.Format("{0}", count) + " Results"; 回答1: SQLDataReaders are forward-only. You're essentially doing this: count++; // initially 1 .DataBind(); //consuming all the records //next iteration on

connected model and disconnected model in EF

百般思念 提交于 2019-11-30 01:16:37
问题 I'm confused a lot about connected model and disconnected in entity framework . I was using traditional ADO.net ( DataReader for connected model and DataAdapter for disconnected model) and all I know that I use connected model when I have many users need to update or insert together and the disconnected model in a few circumstances when I need to send the data to other process make some operations on the data in memory and send them back to the db . Now I read some articles about connected

SqlDataReader vs SqlDataAdapter: which one has the better performance for returning a DataTable?

蓝咒 提交于 2019-11-29 22:20:35
I want to know which one has the better performance for returning a DataTable . Here for SqlDataReader I use DataTable.Load(dr) Using SqlDataReader : public static DataTable populateUsingDataReader(string myQuery) { DataTable dt = new DataTable(); using (SqlConnection con = new SqlConnection(constring)) { SqlCommand cmd = new SqlCommand(myQuery, con); con.Open(); SqlDataReader dr = null; dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); if (dr.HasRows) { dt.Load(dr); } return dt; } } using SqlDataAdapter : public DataTable populateUsingDataAdapter(string myQuery) { SqlDataAdapter dap =

how to check if a datareader is null or empty

依然范特西╮ 提交于 2019-11-29 22:06:44
I have a datareader that return a lsit of records from a sql server database. I have a field in the database called "Additional". This field is 50% of the time empty or null. I am trying to write code that checks if this field isnull. The logic behind this is: If the field "Additional" contains text then display the info otherwise hide the field. I have tried: if (myReader["Additional"] != null) { ltlAdditional.Text = "contains data"; } else { ltlAdditional.Text = "is null"; } The above code gives me this error: Exception Details: System.IndexOutOfRangeException: Additional Any help would be

“already an open DataReader” exception with nested SqlDataReader in ASP.NET

邮差的信 提交于 2019-11-29 21:36:37
问题 I wanted to use nested SqlDataReader in the code below but I couldn't make it.I get "System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first" with the code. Any Suggestions c = "select user_Reps.rep_key,Officers.Officer_Name from user_Reps left join Officers on user_Reps.rep_key = Officers.Officer_code where [user_key]="+userCode; if (c == null) c = sr.ReadToEnd(); try { SqlCommand cmd = new SqlCommand(c, cn); cn.Open();

Slow performance of SqlDataReader

此生再无相见时 提交于 2019-11-29 13:35:54
问题 I've query executing ~2 secs in MSSMS (returning 25K of rows) Same query used in .NET (sqlReader) exetuting few minutes! I've also tried to execute only reader (commented all code in while loop just leaving reader.Read() ) - still same! Any idea what's up? I'm not DBA and not priviledged to play with Profiler - will ask my DBA and let all know. In the meantime I'm noticed essential performance boost after adding " WITH RECOMPILE " param to SP I'm talking So, from my perspective it seems to be

Unable to cast object of type 'System.Int32' to type 'System.String' in DataReader.GetString()

大兔子大兔子 提交于 2019-11-29 10:18:10
I was trying to add data from a database to acombobox. try { SqlCeCommand com = new SqlCeCommand("select * from Category_Master", con); SqlCeDataReader dr = com.ExecuteReader(); while(dr.Read()){ string name = dr.GetString(1); cmbProductCategory.Items.Add(name); } } catch(Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message, System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } I get the following exception: Unable to cast object of type 'System.Int32' to type 'System.String' What am I missing here? Obviously your column doesn't have the type