How to get DB2 Table details using C# - c#

How I can connect and use DB2 in the C# application?
How I can get the table names of a DB2 database?
How I can see the each column name, its datatype, size, etc of a table using C#?

This assumes you're using DB2 for Linux/Unix/Windows. If you're using another platform, it will likely have a different catalog table.
Once you have connected to the database, you can query the information from the system catalog, specifically SYSCAT.COLUMNS.
For example, if you have a table called SCHEMA.TABLE, you could get the list of all the columns using a query like this:
SELECT *
FROM SYSCAT.COLUMNS
WHERE TABSCHEMA='SCHEMA'
AND TABNAME ='TABLE'

Related

SQL Server pre-deploy script to recreate table new columns and preserve data

I created a project in ASP.NET MVC with a separate database project which I run every time there is a table change. My only problem is that if I add one column for example, it will drop the entire database and recreate it and delete all data in the table.
Does anyone know of a pre-deployment script or a method I can use to add / remove / rename tables or column and at the same time preserve the integrity of my data? i.e keep my data while modifying my database
You can rename columns using SQL Server functions, but doing this risks breaking scripts used by other functions or stored procedures in your database. I don't endorse this practice, so I'm not posting about it below. Adding or removing columns is fair game.
You can add columns to a table by using the following query:
ALTER TABLE [YourTable]
ADD [ColumnName] [Datatype];
And you can drop columns using this query:
ALTER TABLE [YourTable]
DROP COLUMN [ColumnName];
These SQL commands will preserve the other columns in your table. If you want to change a column name I encourage you to set up a View in your SQL Server client and give the column an alias.
This can be accomplished using:
CREATE VIEW [ViewName]
AS
SELECT [ColumnName] AS [ColumnAlias]
FROM [TableName]
GO
You'd be able to perform SELECTS on the view in just the same way you can query SELECT on a normal table, except you can query the [ColumnAlias] instead of the [ColumnName]. You cannot perform INSERT or DELETE queries on a view, however

How to iterate and export all tables in a SQL Server database?

Our tests traces are written into a SQL Server database such that every test gets it's own table: log_table_<test_name>.
All the traces are written into the same database.
Is there anyway to iterate over all the tables and export each of them to file via C#?
Just fire a query over tables and you will get all tables that match your test table name format:
select *
from information_schema.tables
where table_name like 'log_table_%'
Another option is to use sys.all_objects:
select *
from sys.all_objects
where type = 'U'
and name like 'log_table_%'
You can check the columns returned in the data set, or check the information_schema.columns or sys.all_columns view for the column names.
You can use the SqlConnection.GetSchema method to retrieve all the tables such as the following
sqlConnection.Open();
DataTable tables = sqlConnection.GetSchema("Tables");
sqlConnection.Close();
kindly note that the returned data table contains both tables and views however there is a column table_type, which tells you whether it's a VIEW or a BASE TABLE.

Cannot insert data using tableadapter.insert method due to an Auto-Increment field

I need your help since I cannot locate an answer anywhere on the web for my problem.
I'm using C# and I have a table called "People" and I want to use an TableAdapter to add/delete to/form that table. I'm using an sdf file as my database as a "Microsoft SQL Server Compact 4.0 (.NET Framework Data Provider for Microsoft SQL Server Compact 4.0)" Data Source.
my code looks like this:
*peopleTableAdapter.Insert(0, byte.Parse(cbAddType.SelectedIndex.ToString()), txtAddName.Text, txtAddCompany.Text, txtAddPhone.Text,
txtAddMobile.Text, txtAddEmail.Text, txtAddAddress.Text, txtAddNotes.Text);
peopleTableAdapter.Update(this.hisabati_DBDataSet.People);*
the table contains a field called "ID" which is an auto-increment field with the following
attributes:
Allow Nulls: No
Unique: Yes
PK: No
the first param in the Insert method is asking for the ID, and if I don't enter a value, I'll get a compile error that a value is needed. If I enter a value (as I'm entering 0 above) I get the following error:
Err Msg: Cannot modify that column
Err HRESULT: -2147467259
I know how to insert using Command.ExecuteNonQuery method, but I'm trying to use the TableAdapters throughout my application as it looks like a more elegant way to write and maintain the codde...Any Advice?
Thanks much
You need to change the InsertCommand and UpdateCommand of the table adapter. Remove the Auto-Increment column name and values from the set part of those commands.
Instead of using that method specifically, create a new row via your People Dataset, then use the TableAdapter to update from that.

SQLite Attach command and Linq to SQL

If you open a sqlite-database from a file you can later attach another file an give it a name, in case it contains equally named tables as the first database.
Using DataContext.GetTable() will return the table of type T from the "original" database. Is there a way to get the other table as well?
How would you do something like "INSERT INTO Person SELECT * FROM Source.Person" in linq, if "Source" is the database you attached?

How to build Query dynamically using asp.net3.5 and c#.net

i need create query dynamically in my .cs page. Actually what happens in my application is user can pass the column name and Table names from the page. by using those parameters(columns and tables) we need build Select query . we need retrieve those values from database and show in text format to user.there may multiple table and single table have multiple columns also.
please try yo help how to do this ny using asp.net3.5 and c3.net
So you need to have a dynamic query that populates its columns as per users requirement. It is not achievable but I have a nearby solution of it.
You can specify from which tables you want data and then using system functions create dynamic query of yours and get it done.
If nothing else, just create the query string dynamically and then pass it to the ADO.NET routines.
string qry = String.Format("SELECT {0} FROM Table", colName);

Categories

Resources