I am using ADO.NET. When I want to save data to table in SQL I need to retrieve columns information in this table. By information I mean Column max size (I want to get 10 from nvarchar(10) column) and NULL or NOT NULL.
I am using next code:
var selectFromCmd = SqlCommandFactory.CreateCommand("select top 0 * from [dbo]." + destTableName, SqlConnection, SqlTransaction);
var dataAdapter = new SqlDataAdapter(selectFromCmd);
var destinationTable = new DataTable();
dataAdapter.Fill(destinationTable);
Then I get DataColumn like so:
var column = destinationTable.Columns["MyColumn"]
But AllowDBNull is always true
and MaxLength is always -1 even if this column is string
So, how can I get the correct information about column properties in ADO.NET ?
I would rather use the sys cataloge views for this query. Something like this....
SELECT c.name ColumnName
,t.Name Datatype
,c.max_length MaxLength
,c.is_nullable
FROM sys.columns c
INNER JOIN sys.types t ON c.user_type_id = t.user_type_id
WHERE c.object_id = object_id('Customers') --<-- your table name
Related
When i am running query in data base and fill data table with load method first column name coming with table name.column name(Employee.Name,sal,location) rest of the column name coming only column names present in the database.Why table name appending first column name while displaying in data table structure please help me in this regards.
DataTable table = new DataTable();
try
{
using (IDbCommand DbCommand = dbConnection.CreateCommand())
{
DbCommand.CommandText = query;
IDataReader reader = DbCommand.ExecuteReader();
table.Load(reader);
}
}
Sample query Select Employee.Numnber, Employee.salary, Employee.city,dept.deptId from Employee inner join dept where deptiId=EmployeeDeptId
The actual query is :
Select
AP_LINE.LINE_NO, AP_LINE.PRODUCT, AP_LINE.QTY, AP_LINE.REQUESTED_DATE,
AP_LINE.LIST_PRICE, AP_LINE.CONS_NET_MULT,AP_LINE.NETADDERS,
AP_LINE.CONS_NET_PRICE, AP_LINE.Details,TDP_JSP.ID, TDP_JSP.LINE_NO,
TDP_JSP_AppInfo.Id
From AP_LINE, CONFIG, TDP_JSP
inner join TDP_JSP_AppInfo on TDP_JSP.ID= TDP_JSP_AppInfo.ID
where AP_LINE.LINE_NO = TDP_JSP.LINE_NO ;
Out put in data table as coming below
Ap_Line.Line_No,Product,QTY,Requested Date...
There are two LINE_NO columns in the query, AP_LINE.LINE_NO and TDP_JSP.LINE_NO. Columns must have unique names, so DataTable.Load used the two-part name wherever needed. It that wasn't possible it would start appending indexes or even generate names like Column0, Column1 etc.
In this case though, one of those columns isn't needed. The two tables are join those columns. WHERE AP_LINE.LINE_NO = TDP_JSP.LINE_NO is the old-style, frowned-upon join syntax. Those two columns will always have the same data so one of them can be removed.
I am trying to retrieve value directly from data table and inserting into a new database table.
I have to get values from more than one table. So I have used INNER JOIN Query to get value from multiple table. The values are getting on data table "dt". Now I want to insert those values into my new table. But while using the given below code showing one error. Help me to find a proper solution. Thank You.
Code:
MailTableAdapters.tbl_MailTableAdapter EM;
EM = new MailTableAdapters.tbl_MailTableAdapter();
DataTable dt = new DataTable();
dt = EM.GetEmpMail();
foreach (DataRow r in dt.Rows)
{
string SiteName = r["Site_Name"].ToString();
string SiteID = r["Site_ID"].ToString();
string AssingnedTeam = r["Assingned_Team"].ToString();
string AssignedList = r["Assigned_List"].ToString();
string EmpID = r["Emp_ID"].ToString();
string EmpName = r["Employee_Name"].ToString();
string EMail = r["Email"].ToString();
}
SQL:
SELECT Site_Name, Site_ID, Assigned_Team, Assign_List, Emp_ID, Employee_Name, Email FROM tbl_AutoAssignCadTeam INNER JOIN tbl_MailTable ON tbl_MailTable.Team = tbl_Employee.Team INNER JOIN tbl_Emp ON tbl_EmployeeToTeam.Emp_ID = tbl_Emp.Emp_ID
Well, you select Assign_List from the database, but use Assigned_List in your code. Assigned_List is not in the columns selected from the database.
What does the dataRowFilter do? is it only a condition for the rows? because adding a select in the row filter does not work
The code is:
Dim strExpr = "ClientID>(select count(*) from Client)-10" (to select the last 10 records);
Dim dv = ds.Tables(0).DefaultView;
dv.RowFilter = strExpr;
Dim newDS = New DataSet();
Dim newDT = dv.ToTable();
newDS.Tables.Add(newDT);
But the code does not work , if I put in strExpr="ClientID>3" the code does work so is the RowFilter only a condition? because I cant put select in it.
The DataRowFilter property is used to filter which rows are viewed in the DataView. The string is internally parsed into DataExpressions. You'll find more information about the expression syntax over at MSDN when looking at the Expression property of the DataColumn class. The same syntax applies to the Select function of the DataTable.
In these LINQ times it's quite trivial to query a data-set/table. For instance, if you have a Client table with indices ranging from 1 to 100, then the following code will return clients with indices 31 to 40.
Dim table As DataTable = (
From row As DataRow
In ds.Tables("Client")
Let clientId As Int32 = row.Field(Of Int32)("ClientID")
Where clientId > 30
Order By clientId Ascending
Select row
).Take(10).CopyToDataTable()
PS: The same query can actually be written as a one-liner:
Dim table As DataTable = (From row As DataRow In ds.Tables("Client") Let clientId As Int32 = row.Field(Of Int32)("ClientID") Where clientId > 30 Order By clientId Ascending Select row).Take(10).CopyToDataTable()
For example my datatable is like this
A_1 A_2 A_3 ..... A_15 B_1.....B_10 C_1....C_10
x y z........ K
1 2 3.........4
I am trying to create seperate datatables for A,B and C which selects the rows based on column prefix, Also i just need row values in my new datatable.
var query = (from dc in table.Columns.Cast<DataColumn>()
where dc.ColumnName.Contains(prefix)
select table.Rows);
If the above is correct, how to proceed to insert the rows(which is in the query) to the new data table ?
You can create a DataView then copy to a DataTable selecting the columns that match your criteria:
string[] cols = (from dc in table.Columns.Cast<DataColumn>()
where dc.ColumnName.Contains(prefix)
select dc.ColumnName)
.ToArray();
DataView view = new DataView(table);
DataTable selected = view.ToTable(false, cols); // false ==> include "duplicate" rows
I am trying to perform a two-pass search against a Sharepoint list. In the first pass, I am using a KeywordQuery to search against the index of all the items. In the second pass, I am applying column value filters chosen by a user by building a select statement.
ResultTableCollection rtc = kwqry.Execute();
ResultTable rt = rtc[ResultType.RelevantResults];
dt = new DataTable();
//Load Data Table
dt.Load(rt, LoadOption.OverwriteChanges);
DataRow[] rows = dt.Select("ColumnName1 = 'foo' AND ColumnName2 = 'bar'");
Where the columns could be multi-value lookup columns in a Sharepoint list. The first pass is working properly and returning the right number of matches in a DataTable. However, when I try to apply the Select statement, I get the following error: Cannot perform '=' operation on System.String[] and System.String. Converting the columns to a string instead of a string array results in the same error, as does using the IN operator.
Am I building my select statement incorrectly? How could I run my second pass filter on my DataTable?
Have you tried with LINQ?
DataTable t1 = new DataTable();
var rows = from x in t1.AsEnumerable()
where x.Field<string[]>("column1name").Contains("foo")
select x;
You have to specify the field type in the Where clause...
Hope it helps.
Try using this, it will work :
DataRow[] rows = dt.Select("(ColumnName1 = 'foo' AND ColumnName2 = 'bar')");