Checking if column allows null values, C#? - c#

How do I check if a specified column allows null values or not?
I'm using the following code to print all the columns, but I also want to print if the column allows null values:
cnn = new SqlConnection(connetionString);
cnn.Open();
SqlCommand myCommand = new SqlCommand("select * from " + tableName, cnn);
SqlDataAdapter da = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
da.Fill(ds, tableName);
foreach (DataColumn dc in ds.Tables[0].Columns)
{
// Print stuff here, dc.ColumnName is the column name
}
The DataColumn.allowDBnull property doesn't seem to work when getting a predefined table, it's always set to true, even in columns that doesn't allow nulls.
Thanks for your time!

If you are only after column data I would do this from the system views, rather than relying on the data adapter. e.g.
DECLARE #TableName VARCHAR(50) = 'dbo.TableName'
SELECT Name, Column_ID, Is_Nullable
FROM SYS.COLUMNS
WHERE [Object_ID] = OBJECT_ID(#TableName)
This also means you can use parameterised queries properly and avoid the risks of SQL Injection. So your final code would be something like this:
using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand("SELECT Name, Is_Nullable FROM sys.columns WHERE [Object_ID] = OBJECT_ID(#TableName)", connection))
{
connection.Open();
command.Parameters.Add("#TableName", SqlDbType.VarChar).Value = tableName;
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine("Name: {0}; Allows Null: {1}", reader.GetString(0), reader.GetBoolean(1));
}
}
}

SqlDataAdapter.Fill() Adds or refreshes rows and does nothing in relation to a table schema information which knows whether particular column allows null. But you can use SqlDataAdapter.FillSchema() to load schema information, then AllowsDBNull would show you a correct state of a column.
SqlDataAdapter da = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
da.FillSchema(ds, SchemaType.Source, tableName);
da.Fill(ds, tableName);

da.FillSchema(ds, SchemaType.Source, tableName);//Loads all the constraints and relations of tables
da.Fill(ds, tableName);//Loads the data

cnn = new SqlConnection(connetionString);
cnn.Open();
SqlCommand myCommand = new SqlCommand("
select name,is_nullable from sys.columns where object_id=object_id('"+tableName+"')", cnn);
SqlDataAdapter da = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
da.Fill(ds, tableName);
foreach (datarow in ds.Tables[0].rows)
{
if(dr["is_nullable"].ToString()==1)
//the column is nullable
else
//column is not nullable
}

Related

Select table using ListBox

I'm trying to read tables in an SQL statement. The names of the Tables, are pulled in a listbox.
Users will select the name of the table they want to view the data for, and in turn my method will read+visualize+export that table.
The issue is with the name of the table as a dynamic value. Initially I wrote the code in IronPython, and was ok.
Now I'm translating this to C# and I face a syntax issue (obviously, the server address/login posted here, are not the real ones).
The tables names are populated in listBox1 from a separate method.
private void rEADSELECTEDTABLE_Click(object sender, EventArgs e)
{
string tableName = listBox1.GetItemText(listBox1.SelectedItem);
MessageBox.Show(" Table Selected: " + tableName);
try
{
string sqlConnectionString;
myConnectionString = #"server=000.00.000.0;database=myDatabase;uid=myUser;password=myPassword";
mySQL = new SqlConnection(myConnectionString);
mySQL.Open();
SqlCommand myCommand2 = new SqlCommand("SELECT * FROM #myTable", mySQL);
SqlParameter param = new SqlParameter();
param.ParameterName = "#myTable";
param.Value = tableName;
myCommand2.Parameters.Add(param);
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.SelectCommand = myCommand2;
DataSet dataSet = new DataSet();
myAdapter.Fill(dataSet);
List<string> rows = new List<string>();
List<string> rowData = new List<string>();
foreach (DataTable table in dataSet.Tables)
foreach (DataRow row in table.Rows)
foreach (DataColumn column in table.Columns)
if (row[column] != null)
rowData.Add(row[column].ToString());
foreach (String s in rowData)
Console.WriteLine(s);
mySQL.Close();
}
}
When I run the code I get this error:
System.Data.SqlClient.SqlException (0x80131904): Must declare the table variable "#myTable".
If I use a static table name, everything works well.
SqlCommand myCommand2 = new SqlCommand("SELECT * FROM TABLE_NAME", mySQL);
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.SelectCommand = myCommand2;
Help greatly appreciated.
you cannot use TableName as a parameter with SqlCommand
change the sql query, somthig like :
SqlCommand myCommand2 = new SqlCommand("SELECT * FROM " + tableName, mySQL);
OR
SqlCommand myCommand2 = new SqlCommand(string.Format("SELECT * FROM {0}", tableName), mySQL);
but be careful from sql injection attack !
another way is to work with dynamic sql query using stored procedure and send the table name as a parameter

Dataset filter method

Here is my code,
Conn.Open();
DataTable dt = new DataTable();
DataSet ds = new DataSet();
SqlCommand sqlCmd = new SqlCommand("SELECT * from CurrentDataCR ",Conn);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(ds);
ds.Tables[0].DefaultView.RowFilter = " mst_remote_station_id Like'*9001*'";
Here I am getting Complete row for id 9001. I need only one column value for this id.
DataRow[] rows = ds.Tables[0].Select("mst_remote_station_id Like '%9001%'");
You can do it this way also if you need only one row just select it in the initial query.
Also you should Dispose the SqlDataAdapter after using it ! You can do it with using block
Conn.Open();
DataSet ds = new DataSet();
SqlCommand sqlCmd = new SqlCommand("SELECT * from CurrentDataCR ",Conn);
using(SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd))
{
sqlDa.Fill(ds);
}
ds.Tables[0].Select("mst_remote_station_id Like '%9001%'");
I don't know if the connection is global but it is bad practice to use global connection, you have connection pool so use separate connection for every query.

A field or property with the name <variable> was not found on the selected data source

It's my first time to implement INNER JOIN query in SQL.NET and C#.NET. I get this error:
A field or property with the name 'Prep_By' was not found on the selected data source.
I don't understand what is the problem, the field 'Prep_By' is existing in my database.
Here's what I got:
private void LoadFeedback()
{
con = new SqlConnection(Connectiontxt);
con.Open();
SqlCommand cmd;
if (seardata == "")
{
cmd = new SqlCommand("SELECT [Articles_Tbl].[Article_ID], [Articles_Tbl].[Title], [Articles_Tbl].[Mod_Date], [Users_Tbl].[Name] FROM [Articles_Tbl] INNER JOIN [Users_Tbl] ON [Users_Tbl].[User_ID] = [Articles_Tbl].[Prep_By] where [Articles_Tbl].[Status] = 'Approved' and (Article_ID = '')", con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "ss");
gridrfqheader0.DataSource = ds.Tables["ss"];
gridrfqheader0.DataBind();
}
else {
cmd = new SqlCommand("SELECT [Articles_Tbl].[Article_ID], [Articles_Tbl].[Title], [Articles_Tbl].[Mod_Date], [Users_Tbl].[Name] FROM [Articles_Tbl] INNER JOIN [Users_Tbl] ON [Users_Tbl].[User_ID] = [Articles_Tbl].[Prep_By] where [Articles_Tbl].[Status] = 'Approved' and (Article_ID LIKE '%" + seardata + "%' or Title LIKE '%" + seardata + "%')", con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "ss");
gridrfqheader0.DataSource = ds.Tables["ss"];
gridrfqheader0.DataBind();
}
}
It means that gridrfqheader0 contains a binding reference to Prep_By, but you are not including it in your SELECT statement.
Try adding it:
cmd = new SqlCommand("SELECT [Articles_Tbl].[Prep_By], [Articles_Tbl].[Article_ID])...
As a side note, your conditional statements contain a lot of duplicate code. Consider moving the code that gets data into one location so that you do't have duplicate code. For example:
if (isNullOrEmpty(seardata))
{
cmd = new SqlCommand(your query);
}
else
{
cmd = new SqlCommand(your other query);
}
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "ss");
gridrfqheader0.DataSource = ds.Tables["ss"];
gridrfqheader0.DataBind();
It can be further refactored, but this is a good start.
You are using Dataset to bind Grid, and in Grid you are using [Prep_By] field which not in your dataset.
So Simply just
Add "[Articles_Tbl].[Prep_By]" in your select statenment.
cmd = new SqlCommand("SELECT [Articles_Tbl].[Prep_By], [Articles_Tbl].[Article_ID], [Articles_Tbl].[Title], [Articles_Tbl].[Mod_Date], [Users_Tbl].[Name] FROM [Articles_Tbl] INNER JOIN [Users_Tbl] ON [Users_Tbl].[User_ID] = [Articles_Tbl].[Prep_By] where [Articles_Tbl].[Status] = 'Approved' and (Article_ID = '')", con);
make change in both sql queries..
If the grid has the column created with the source name Prep_By you have to select that column from the database.

How to Use an Update Statement in SQLDataAdapter

I am trying to run an update statement after i built my sqldataadapter. I have column called INIT_PHASE in my table and if the INIT_PHASE is null or there is no data then i would like to set it to 1. I have tried but i can't seem to get it right the update statement. Pls. help. here is my code:
string ID = ddlPractice.SelectedValue;
string TYPE = DDL_TYPE.SelectedValue;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter(#"select SET_SK, UNIT_NM, TYPE, INIT_PHASE FROM myTable WHERE UNIT_NM =#ID AND TYPE = #TYPE", con);
DataTable dtSETS = new DataTable();
da.SelectCommand.Parameters.AddWithValue("#ID", (ID));
da.SelectCommand.Parameters.AddWithValue("#TYPE", (TYPE));
da.Fill(dtSETS);
if (dtSETS.Rows.Count > 0)
{
DataRow dtSETS_row = dtSETS.Rows[0];
long SET_SK = dtSETS_row.Field<long>("SET_SK");
if (dtSETS_row.Field<string>("INIT_PHASE") == null)
{
//run update command here
update myTable set INIT_PHASE = 1;
}
}
One approach here would be to use the SqlCommandBuilder to build the UPDATE statement:
string ID = ddlPractice.SelectedValue;
string TYPE = DDL_TYPE.SelectedValue;
SqlConnection con = new SqlConnection(
ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter(
#"select SET_SK, UNIT_NM, TYPE, INIT_PHASE FROM myTable WHERE UNIT_NM =#ID AND TYPE = #TYPE",
con);
DataTable dtSETS = new DataTable();
da.SelectCommand.Parameters.AddWithValue("#ID", (ID));
da.SelectCommand.Parameters.AddWithValue("#TYPE", (TYPE));
da.Fill(dtSETS);
SqlCommandBuilder builder = new SqlCommandBuilder(da);
da.UpdateCommand = builder.GetUpdateCommand();
if (dtSETS.Rows.Count > 0)
{
DataRow dtSETS_row = dtSETS.Rows[0];
long SET_SK = dtSETS_row.Field<long>("SET_SK");
if (dtSETS_row.Field<string>("INIT_PHASE") == null)
{
dtSETS_row["INIT_PHASE"] = 1;
}
}
da.Update(dtSETS);
Take note to the following lines of code. Here we are building the update command:
SqlCommandBuilder builder = new SqlCommandBuilder(da);
da.UpdateCommand = builder.GetUpdateCommand();
here we are literally modifying the DataRow so that it's RowState is changed to Modified:
dtSETS_row["INIT_PHASE"] = 1;
and then finally, here we are sending updates to the database with the Update method on the SqlDataAdapter:
da.Update(dtSETS);
What this is going to do is only send updates for the rows with a RowState of Modified.
NOTE: each of the ADO.NET objects should be wrapped in a using. Refactor your code to match this type of template:
using (SqlConnection con = new SqlConnection(...))
{
using (SqlDataAdapter da = new SqlDataAdapter(...))
{
}
}
If I understand correctly, you could execute directly a command to update just this field
if (dtSETS_row.Field<string>("INIT_PHASE") == null)
{
SqlCommand cmd = new SqlCommand(#"UPDATE myTable set INIT_PHASE = 1 " +
"WHERE UNIT_NM =#ID AND TYPE = #TYPE", con);
cmd.Parameters.AddWithValue("#ID", (ID));
cmd.Parameters.AddWithValue("#TYPE", (TYPE));
cmd.ExecuteNonQuery();
}
You need to open the connection though both for the SqlDataAdapter and for the following command
You will have to use SqlCommandBuilder class for updating data in disconnected mode. ie) DataSet and Data Adapters.

ADO.NET - Updating Multiple DataTables

So I have some code like this:
DataSet dataSet = new DataSet();
DataTable dataTable1 = new DataTable("Table1");
DataTable dataTable2 = new DataTable("Table2");
DataTable dataTable3 = new DataTable("Table3");
DataTable dataTable4 = new DataTable("Table4");
dataSet.Tables.Add(dataTable1);
dataSet.Tables.Add(dataTable2);
dataSet.Tables.Add(dataTable3);
dataSet.Tables.Add(dataTable4);
SqlDataAdapter dataAdapter1 = new SqlDataAdapter("SELECT * FROM Table1 WHERE ID = 1", sqlConnection);
SqlDataAdapter dataAdapter2 = new SqlDataAdapter("SELECT Column1, Column2, Column3 FROM Table2", sqlConnection);
SqlDataAdapter dataAdapter3 = new SqlDataAdapter("SELECT Column1, Column2, Column3 FROM Table3", sqlConnection);
SqlDataAdapter dataAdapter4 = new SqlDataAdapter("SELECT Column1, Column2, Column3 FROM Table4", sqlConnection);
SqlCommandBuilder commandBuilder1 = new SqlCommandBuilder(dataAdapter1);
SqlCommandBuilder commandBuilder2 = new SqlCommandBuilder(dataAdapter2);
SqlCommandBuilder commandBuilder3 = new SqlCommandBuilder(dataAdapter3);
SqlCommandBuilder commandBuilder4 = new SqlCommandBuilder(dataAdapter4);
dataAdapter1.Fill(dataTable1);
dataAdapter2.FillSchema(dataTable2, SchemaType.Source);
dataAdapter3.FillSchema(dataTable3, SchemaType.Source);
dataAdapter4.FillSchema(dataTable4, SchemaType.Source);
//do a bunch of code that updates the one row from Table1
//and adds lots of new rows to Table2, Table3, Table4
dataAdapter1.Update(dataTable1);
dataAdapter2.Update(dataTable2);
dataAdapter3.Update(dataTable3);
dataAdapter4.Update(dataTable4);
dataSet.AcceptChanges();
Is there anyway to make this a lot simpler? What would happen if the computer crashed on the line after "dataAdapter2.Update(dataTable2);"? I would like to be able to somehow use just one Update call to update everything. Is that possible?
Also, is this even the best way to do this? With "this" being creating a bunch of new rows in multiple tables depending on what is in one specific row in one specific table.
You can pass a dataset into a DataAdapter's Update statement, which will update all of the tables in the dataset. UPDATE: No, it doesn't. DataAdapters always update only one table. The overload to Update() that takes a DataSet as its parameter, from the MSDN documentation, "Calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified DataSet from a DataTable named "Table"." Sorry for the confusion. The rest of the answer is still valid, though.
If you want to assure that all the updates succeed or fail as an atomic unit, use the SqlTransaction object:
DataSet ds = new DataSet();
// do something with the dataset
SqlDataAdapter dataAdapter = new SqlDataAdapter();
SqlConnection cn = new SqlConnection(connString);
cn.Open();
SqlTransaction trans = cn.BeginTransaction();
SqlDataAdapter dataAdapter = new SqlDataAdapter();
// set the InsertCommand, UpdateCommand, and DeleteCommand for the data adapter
dataAdapter.InsertCommand.Transaction = trans;
dataAdapter.UpdateCommand.Transaction = trans;
dataAdapter.DeleteCommand.Transaction = trans;
try
{
dataAdapter.Update( ds );
trans.Commit();
}
catch
{
trans.Rollback();
}
cn.Close();

Categories

Resources