C# 在SQL的TIB表查询有没有这个字段Edit,如果有则插入字段
时间: 2024-03-17 20:41:26 浏览: 79
你可以使用以下代码来查询TIB表中是否存在Edit字段,如果存在则插入字段:
``` csharp
using System.Data.SqlClient;
string connectionString = "Data Source=<server>;Initial Catalog=<database>;User ID=<username>;Password=<password>";
string tableName = "TIB";
string columnName = "Edit";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand($"SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{tableName}' AND COLUMN_NAME = '{columnName}'", connection);
int count = (int)command.ExecuteScalar();
if (count == 0)
{
command = new SqlCommand($"ALTER TABLE {tableName} ADD {columnName} <data type>", connection);
command.ExecuteNonQuery();
}
connection.Close();
}
```
请注意,你需要将`<server>`,`<database>`,`<username>`,`<password>`和`<data type>`替换为你的实际值。此外,如果要插入的字段类型为字符串,则需要将`<data type>`替换为适当的字符串类型,如`NVARCHAR(MAX)`。
阅读全文