出示sqlserver的代码
时间: 2023-07-09 15:37:32 浏览: 46
好的,以下是一些SQL Server数据库操作的代码示例。
1. 连接数据库
```
string connectionString = "Data Source=(local);Initial Catalog=LabManagement;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
```
2. 创建数据库
```
string connectionString = "Data Source=(local);Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
string sql = "CREATE DATABASE LabManagement";
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
```
3. 创建表
```
string connectionString = "Data Source=(local);Initial Catalog=LabManagement;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
string sql = "CREATE TABLE Equipment (ID INT PRIMARY KEY IDENTITY(1,1), Name VARCHAR(50), Type VARCHAR(50), LabID INT)";
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
```
4. 插入数据
```
string connectionString = "Data Source=(local);Initial Catalog=LabManagement;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
string sql = "INSERT INTO Equipment(Name, Type, LabID) VALUES(@Name, @Type, @LabID)";
SqlCommand command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("@Name", "设备1");
command.Parameters.AddWithValue("@Type", "类型1");
command.Parameters.AddWithValue("@LabID", 1);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
```
5. 查询数据
```
string connectionString = "Data Source=(local);Initial Catalog=LabManagement;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
string sql = "SELECT * FROM Equipment WHERE LabID = @LabID";
SqlCommand command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("@LabID", 1);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
int id = reader.GetInt32(0);
string name = reader.GetString(1);
string type = reader.GetString(2);
int labID = reader.GetInt32(3);
Console.WriteLine("ID: {0}, Name: {1}, Type: {2}, LabID: {3}", id, name, type, labID);
}
reader.Close();
connection.Close();
```
以上代码示例仅供参考,具体实现还需要根据您的具体需求进行调整和完善。希望能对您有所帮助。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)