Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I work on a project where I gather data from an SQL server.
I need to read 2 columns out of the database. The amount of rows can differ each time.
What I want to do is create variables modular to the amount of rows it has read.
An example:
I have 2 columns with the names: Name, Status.
In this table I have 10 rows. What I want my code to do is when it reads the data, it needs to create variables with the names from the Column Name and with the value of the column status.
So that would mean that when my code is done I have 10 new variables with the Names from Column: Name. And the values of those variables are the values from the column: Status.
Is there a proper way to do this?
Edit here is my test code:
while (reader.Read())
{
string F_Name = reader.GetString(0);
string status = reader.GetString(1);
list.Add(F_Name);
list.Add(status);
}
You can try using a dictionary like this:
var dict = new Dictionary<string, string>();
foreach (var row in rowsFromDb)
{
string name = row["Name"].ToString();
string status = row["Status"].ToString();
dict.Add(name, status);
}
A Dictionary is a data structure that could have many entries, and each entry has an index and a value, so they are linked (as you want).
And you can access the values of a dictionary this way:
string status = dict["VariableName"];
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am trying to retrieve the data from a DataTable within a DataSet and store it into an array.
Previously I was just retrieving and storing the data from a DataTable into an array using the following code.
var stringArr = DtTable.AsEnumerable().Select(r => r.Field<string>("ExampleId")).ToArray();
and it worked. So I decided to modify it to access the DataSet like so
var stringArr = DtSet.Tables["Results"].TableName.AsEnumerable().Select(r => r.Field<string>("ExampleId")).ToArray();
I now get the following error message and is unsure how to fix
Error CS1929 'char' does not contain a definition for 'Field' and the best extension method overload 'DataRowExtensions.Field(DataRow, string)' requires a receiver of type 'DataRow
The problem is that you added TableName in the chain.
You are basically asking to turn the TableName in an IEnumerable (which returns an IEnumerable<char>) and for every char of the table name string your are trying to call .Field which of course does not exist.
You need to write:
//Notice TableName being removed
var stringArr = DtSet.Tables["Results"].AsEnumerable().Select(r => r.Field<string>("ExampleId")).ToArray();
Removing TableName from the chain will return the IEnumerable<DataRow> that you need.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Hi sp returns 2 tables set record how to marge result set.
Result set table 1:
Result set table 2:
dtAll = new DataTable();
...
dtAll.Merge(dtOne);
dtAll.Merge(dtTwo);
dtAll.Merge(dtThree);
...
and so on.
This technique is useful in a loop where you want to iteratively merge data tables:
DataTable dtAllCountries = new DataTable();
foreach(String strCountry in listCountries)
{
DataTable dtCountry = getData(strCountry); //Some function that returns a data table
dtAllCountries.Merge(dtCountry);
}
If iam understanding you correctly
You wish to merge 2 identical data tables
dataTable.Merge(dataTable1);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a table named product where there is a field named id its the primary key here. I need to put this key to my another table pro_size_tbl as below
pro_size_tbl
==================
id
productid
sizeid
the matter is when the user adds a product the sizes of the products are saved in session and I am trying to save them in pro_size_tbl by the product key what I just made.
The thing I am doing right now is I am adding the product in the product table and in the next line I am retrieving the max id of the product table and using it as the key for the pro_size_tbl.
My question is is there any better way to do this thing?
This is simple.You can use LAST_INSERT_ID().See this sample:
INSERT INTO table1 (title,userid) VALUES ('test', 1);
SET #last_id_in_table1 = LAST_INSERT_ID();
There is a function LAST_INSERT_ID() which can give you the desired value.
But you need to use right below the insert statement.
owh. from what i understood, u need to put auto generated mysql id to another table?
if so, here is the real deal
$sql_tbl1 = "insert here for first table";
$result_tbl1 = mysql_query($sql_tbl1) or die(mysql_error());
$id = mysql_insert_id();
$sql_tbl2 = "insert here value ($id, 'bla', 'bla')";
mysql_query(sql_tbl2);
LAST_INSERT_ID() function is ok but it is doing the same work as i am doing taking the maximum value.i guess more or less both way are the same.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a table that stores information of all the contacts that were added for sending SMS campaign.
Every SMS is associated with campaign. campaignid is used in the record for maintaining that.
I receive the sent mobile_numbers and campaign_id from service provider once the blast is done for campaign.
Now I need to iterate smscontacts table and do the following.
First Get all the records having campaign_id . Update the table by updating column issent as 1 for all the contacts that have mobile number in mobile_numbers list sent by provider after blast.
Edit:
Find rows that have column value for mobile number column in list mobile_numbers and update other columns value called for these records.
Assuming your MobileNumber property is a string and it is null, empty string or white space when it's not set, do the following.
// Get contacts that have a mobile number
IQueryable<Contact> contactsWithANumber = context.Contacts.Select(x => !string.IsNullOrWhiteSpace(x.MobileNumber));
// foreach
foreach(Contact c in contactsWithANumber)
{
c.IsSent = 1;
}
// .ForEach()
contactsWithANumber.ForEach(c => c.IsSent = 1);
// Update in db.
context.SaveChanges();
You can use ExecuteCommand to update multiple records:
yourContext.ExecuteCommand("UPDATE YourTable SET issent= 1 WHERE YourCondition");
For information see DataContext.ExecuteCommand Method
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to fetch all columns from the Users table, to store in holder as an array so that I can access all the rows from it. But the problem is when I put a breakpoint in the code, the holder variable only shows the value Webmatrix.Data.Database, why is that?
#{
var db = Database.Open("test");
var holder = db.QuerySingle("SELECT * FROM Users WHERE id = 1");
}
It's obviously wrong, so please correct my mistakes :) Thanks!
(edited to include the second double-quote that was in my actual code, but missed out in this question)
Your query is missing the closing double-quotes ("). The following will work:
#{
var db = Database.Open("test");
var holder = db.QuerySingle("SELECT * FROM Users WHERE id = 1");
}