[英]how to check if a row or column is already present in the table while inserting or updating the table
I am building a project in which I access database tables, insert and update rows and columns into the table. 我正在建立一个项目,在其中访问数据库表,在表中插入和更新行和列。
I am updating a table in the database using a GridView
, using which I am adding new column every time to another table. 我正在使用GridView
更新数据库中的表,每次都使用GridView
将新列添加到另一个表中。
My code on button_Click is: 我在button_Click上的代码是:
com = new SqlCommand("ALTER TABLE Location_Profile_Master ADD " + LocProName.Text + " int", con);
con.Open();
com.ExecuteNonQuery();
con.Close();
foreach (GridViewRow row in GridView1.Rows)
{
string Pro_Name = row.Cells[1].Text;
for (int i = 1; i < GridView1.Columns.Count; i++)
{
int n = Convert.ToInt16(row.Cells[i + 1].Text);
//short n = 0;
//if (Int16.TryParse(row.Cells[i + 1].Text, out n))
//{
com = new SqlCommand("UPDATE Location_Profile_Master SET " + LocProName.Text + "='" + n + "' WHERE Profile_Name='" + Pro_Name.ToString().Trim() + "' ", con);
con.Open();
com.ExecuteNonQuery();
con.Close();
//}
}
}
Therefore while adding the column and its values, I want to check if that column name is already present in the database, if not present I want to check if the column values is already present in the database. 因此,在添加列及其值时,我想检查数据库中是否已经存在该列名,如果不存在,我想检查数据库中是否已存在列值。 User should be notified that it column is already present in the data base. 应通知用户数据库中已存在it列。
Also while I am inserting a row, I want to know if that row is already present in the table. 另外,当我插入一行时,我想知道表中是否已经存在该行。
kindly help. 请帮助。
You can read schema informations in MS SQL-Server in the Information Schema Views directly or you can use DataReader.ExecuteReader(CommandBehavior.SchemaOnly)
to retrieve the schema informations of a given table: 您可以直接在MS SQL Server的Information Schema Views中读取架构信息,也可以使用DataReader.ExecuteReader(CommandBehavior.SchemaOnly)
检索给定表的架构信息:
DataTable schema;
using (var con = new System.Data.SqlClient.SqlConnection(conStr))
{
var getSchemaSql = String.Format("SELECT * FROM {0}", tableName);
using (var schemaCommand = new System.Data.SqlClient.SqlCommand(getSchemaSql, con))
{
con.Open();
using (var reader = schemaCommand.ExecuteReader(CommandBehavior.SchemaOnly))
{
schema = reader.GetSchemaTable();
}
}
}
and then something similar to this: 然后类似于以下内容:
for (int rowIndex = 0; rowIndex < schema.Rows.Count; rowIndex ++)
{
DataRow schemaRow = schema.Rows[rowIndex];
String columnName = schemaRow.Field<String>("ColumnName");
Type dataType = schemaRow.Field<Type>("DataType");
Int32 columnSize = schemaRow.Field<Int32>("ColumnSize");
if (dataType.FullName == "System.String")
{
// ...
}
}
or, if you simply want to check if a given column name exists in that table: 或者,如果您只是想检查该表中是否存在给定的列名:
String colName = "Column1";
bool exists = schema.AsEnumerable()
.Any(r => r.Field<String>("ColumnName") == colName);
if you want to know if a row already exists, you could use it's primary-key/identifier and EXISTS
in sql: 如果您想知道行是否已存在,可以在sql中使用它的主键/标识符和EXISTS
:
IF NOT EXISTS(SELECT 1 FROM Location_Profile_Master WHERE PROFILE_NAME=@ProfileName)
You will be better of making a stored procedure for this sort of things like this: 对于这种事情,您最好制作一个存储过程:
IF EXISTS ( SELECT 1
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Location_Profile_Master'
AND Column_Name = @columnParam)
BEGIN
-- The column exists
END
ELSE
-- The column doesn't exit
I assume you are using MS SQL Server. 我假设您正在使用MS SQL Server。 Try this code to check if the column exists and inside it verifies if the row exists: 尝试使用以下代码检查该列是否存在,并在其中验证该行是否存在:
IF EXISTS(SELECT * FROM SYS.COLUMNS WHERE Name = N'columnName'
AND Object_ID = Object_ID(N'Location_Profile_Master'))
BEGIN
-- Column Exists
IF EXISTS (SELECT Profile_Name FROM Location_Profile_Master WHERE Profile_Name = @yourValue)
BEGIN
--value exists
END
END
First of all an advice: use parameters instead of value "'" + "'"
, because it is sensitive againts SQL Injection. 首先建议:使用参数代替值"'" + "'"
,因为它对SQL注入很敏感。
To check in the database that a column is exists or not you can use: 要在数据库中检查是否存在某列,可以使用:
SELECT CASE WHEN COL_LENGTH('tableName', 'columnName') IS NOT NULL
THEN 1 ELSE 0 END AS isExists
This will give you a 0 even if the table is not exists in the database 即使表在数据库中不存在,也会为您提供0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.