I am not the best with oracle sql.
I have List<T> on webservice and I need send list to client side of application.
After that I need write distinct list data in table, and join with few other tables.
For example:
I have define:
public class VIEW_COLOR
{
public int COL_NAME_CODE { get; set; }
public int COL_DESC_CODE { get; set; }
}
I get data with
public List<VIEW_COLOR> GetColor()
{
string sql = "select distinct * from VIEW_COLOR";
IEnumerable<VIEW_COLOR"> loc = _connection.Query<VIEW_COLOR>(sql);
return loc.ToList<VIEW_COLOR"=>();
}
Than i use:
[WebMethod]
public List<VIEW_COLOR> GetColor()
{
return dal.GetLColor();
}
I try this part and I get that in list on webservice list.
I have tables:
Table desc_name have code, tab_col_d_name
Table name_desc have code, tab_col_n_desc
Now i need that application when I input this data to client side , and insert in full_table that have columns:
1. tab_col_name_code
2. tab_col_name_desc
3. tab_col_desc_code
4. tab_col_desc_name
Table desc_name have
1. tab_col_d_name
2. tab_col_d_code
Table name_desc have
1. tab_col_n_desc
2. tab_col_n_code
I need to insert in local full_table tab_col_name_code=COL_NAME_CODE and tab_col_name_desc= tab_col_n_name where tab_col_n_code = COLOR_DESC_CODE (from list) and for second two columns same.
I hope I was clearly with this, I am not best with English and was little hard explain what I try to do.
Q: "I dont know how send list from webserver to client side of apps"
A: Call your server webmethod on client side and get the result.
Q: "how write sql witch wold insert in table "where field = value from list"
A: You may create SQL query dynamically or use some ORM layer to insert data through ORM-generated entities.
Related
I tried to convert this simple sql query to linq to sql query
SELECT * INTO temptable from EFESRDP0
ALTER TABLE temptable DROP COLUMN PASSWORD,SECONDPASS
SELECT * FROM temptable
DROP TABLE temptable
But I couldnt. Anyhelp would be appreciated, thank you.
Since Linq to SQL has no equivalent for the table operations you're trying to perform, the short answer is you can't do that.
From the structure of the query though it looks like the following is happening:
All records from EFESRDP0 are added to a previously non-existent table temptable
A few columns are dropped from temptable
The remaining data is returned as a recordset
The temporary table is dropped
Which is a long-winded way of specifying a list of columns to return from the original table, isn't it? Bad SQL shouldn't be turned into even worse LINQ, it should be fixed.
In query syntax the simple form would be:
var results =
from row in context.EFESRDP0
select new { row.ID, row.Name, row.LastLoginTime /* or whatever */ };
This will result in an SQL query similar to:
SELECT ID, Name, LastLoginTime
FROM EFESRDP0;
Which is a whole lot simpler than the SQL you posted and appears to do basically the same thing without all the table gymnastics.
Since your SQL statements are effectively returning all columns except Password and SECONDPASS, you can do that with a simple Linq like Corey gave creating a new anonymous type. You can also have a type defined with all the columns except those 2 so you could get a typed result. ie: This sample returns only 3 columns from sample Northwind..Customers table:
void Main()
{
DataContext db = new DataContext(#"server=.\SQLexpress;trusted_connection=yes;database=Northwind");
Table<Customer> Customers = db.GetTable<Customer>();
// for LinqPad
//Customers.Dump();
}
[Table(Name = "Customers")]
public class Customer
{
[Column]
public string CustomerID { get; set; }
[Column]
public string ContactName { get; set; }
[Column]
public string CompanyName { get; set; }
}
I'm trying to recieve data from an database table and my sql query also selects a column from another table that i would like to return aswell
while (dr.Read())
{
Inlagg comentsar = new Inlagg
{
names = (int)dr["name"],
dates = (DateTime)dr["Date"]
//get column value from the other table not the Inlagg table here pobbisble?
};
//This gets the column value from the other table but i dont know how to return it
string test = (string)dr["test"];
comen.Add(comentsar);
}
return comen;
How can i return a result that includes columns both columns from different tables? Side note: I have a column that i dont need to recieve here, altough the column is an int and i'm trying to recieve an string, is there any way to cast the column to an string? Any help or input highly appreciated, thanks!
EDIT: "Solved" it by simply creating a new class with the values i wanted which included columnvalues from different tables
Inside of your reader, your missing the following:
if(reader["Column"] != DBNull.Value)
model.Name = reader["Column"].ToString();
Your query for your SQL should correctly join the multiple tables, then in the returned result set you would have a column name to return by. You call the reader["..."] to access the value for said column.
As you notice the model class calls a name of a property.
The model is important as it represents your data, but to access through the reader you simply add another property.
That is because the column is null, so you can't cast a ToString() that is why the error occurs. You could attempt to also do:
int? example = reader["Column"] as int?;
Extend your class:
public class Inlagg{
...
addMyString(String myString){
this.myString = myString;
}
getMyString(){
return this.myString;
}
}
I have seen the answer to this question, How to map multiple records from a single SP with Dapper-dot-net, but it doesn't seem to work for my scenario.
Dummy Tables for illustration...
I have a SP that returns multiple record sets, and the first one looks like...
Column1 (int), Column2 (int)
and I have a class...
public class Columns
{
public int Column1 { get; set; }
public int Column2 { get; set; }
}
Then, I am trying to build a list of columns...
using (var conn = new SqlConnection(...))
{
using (var multi = conn.QueryMultiple("SpData",
commandType: CommandType.StoredProcedure))
{
var cols = multi.Read<Columns>().ToList();
}
}
When I call this, nothing seems to be populated in the cols variable, but I am not getting any errors. Also, if I break and look at what multi contains, I can see the data there. Can somebody see where I am going wrong?
I'm not sure whether to delete the question, or just add this as an answer as I spotted the obvious error (after I woke up)
I wasn't interested in the first two datasets from the SP, so I needed to skip by using Read...
multi.Read();//skip first recordset
multi.Read();//skip second recordset
It might help someone else, otherwise I will delete it soon.
I am using VS2005 C# and SQL Server Database 2005.
I am tying to compare values between 2 databases.
I am able to retrieve the variable [StudentName] from tStudent Table via a SELECT WHERE sql statement, as follow:
Now, I have another table named StudentDetails. It has 3 columns, StudentName,Address and ContactNum:
The situation is that I want to grep the result from the first SQL query on tStudent, which returns me a list of Students whose [Status]=DELETED.
And from the list of Students queried , I want to take one Student at a time, and search through my [StudentDetails] table.
If it exist in [StudentDetails], I wan to use a way to store the variable [StudentName] from StudentDetails table and display it in GridView on my webpage.
(open to many solutions here. store in database; display result in GridView; store in array; etc)
May I know what the ways and steps I can take to achieve the result?
Step by step guide and code snippets are very much appreciated, because I am quite weak in C# programming.
you can do like this:
Use Visual Studio to create a DataSet name StudentDS, create table name "Student" in this DataSet, this table will contain 3 table columns: String StudentName; String Address; String ContactNum;
Fill deleted students into this DataSet:
DataSet dset = new StudentDS();
String connectionString = "";// depends on your database system, refer to http://www.connectionstrings.com
using (connection = new OdbcConnection(connectionString))
{
connection.Open();
command.Connection = connection;
command.CommandText = "select StudentName, Address, ContactNum from tStudent WHERE status = 'DELETE'";
OdbcDataAdapter da = new OdbcDataAdapter();
da.SelectCommand = command;
da.Fill(dset, "Student");
}
- After you get this DataSet, you can iterate on its row to do what you want.
if(dset.Tables[0].Rows != null) {
for (int i = 0; i < dset.Tables[0].Rows.Count, i++){
if(!ExistInStudentDetail(dset.Tables[0].Rows[i]["StudentName"]))
{
dset.Tables[0].Rows.remove(i);
i--;
}
}
}
//here, boolean ExistInStudentDetail(String StudentName) is a method, you can create sql for this as same in above.
In your form, add a new DataGridView name "StudentForm",add 1 column for this DataGridView name "StudentName", and set its binding property to "StudentName" (same column name in DataSet), and then set DataSource of this grid.
StudentForm.DataSource = dSet;
HTH.
This is a fairly simple issues but the scope is pretty large. So here goes:
First you should really make sure you have unique columns in the tables you are searching this allows you to modify those individual rows and make sure that you are modifying the correct one. I didn't see any ID columns in the screenshot so I just wanted to cover this.
Second I would create a class for students. In here I would create fields or properties of all the information that I wanted.
class Student
{
public string Name { get; private set; }
public string Address { get; private set; }
public string ContactNum { get; private set; }
}
you can either use a constructor in the above class and fill the properties with that or you can fill in each through your select your choice.
Third I would create a List<Student> students; this will be used as your reference list
List<Student> deletedStudents = SQL Select Statement;
Fourth I would then create another List<Student> detailedStudents;
Finally I would compare the two lists and then do something when a match is found.
I'm trying to find a better and faster way to insert pretty massive amount of data(~50K rows) than the Linq that I'm using now.
The data I'm trying to write to a local db is in a list of ORM mapped data serialized and received from WCF.
I'm keen on using SqlBulkCopy, but the problem is that the tables are normalized and are actually a sequence or interconnected tables with one-to-many relationships.
Here's some code that illustrates my point:
foreach (var meeting in meetingsList)
{
int meetingId = dbAccess.InsertM(value1, value2...);
foreach (var competition in meeting.COMPETITIONs)
{
int competitionId = dbAccess.InsertC(meetingid, value1, value2...);
foreach(var competitor in competition.COMPETITORs)
{
int competitorId = dbAccess.InsertCO(comeetitionId, value1,....)
// and so on
}
}
}
where dbAccess.InsertMeeting looks something like this:
// check if meeting exists
int meetingId = GetMeeting(meeting, date);
if (meetingId == 0)
{
// if meeting doesn't exist insert new
var m = new MEETING
{
NAME = name,
DATE = date
}
_db.InsertOnSubmit(m);
_db.SubmitChanges();
}
Thanks in advance for any answers.
Bojan
I would still use SqlBulkCopy to quickly copy your data from the external file into a staging table that has the same (flat) structure as the file (you'll need to create that table ahead of time)
Once it's loaded, you can split up the data across multiple tables using e.g. a stored procedure or something - should be pretty fast since everything's on the server already.