cshap将world写入sqlserver
时间: 2023-09-08 10:16:31 浏览: 80
SOAP教程C++CSHAP
以下是C#代码示例,将字符串"world"插入到名为"mytable"的SQL Server数据库表中的"mycolumn"列中:
```csharp
using System.Data.SqlClient;
string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword";
string insertQuery = "INSERT INTO mytable (mycolumn) VALUES (@value)";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(insertQuery, connection);
command.Parameters.AddWithValue("@value", "world");
try
{
connection.Open();
int rowsAffected = command.ExecuteNonQuery();
Console.WriteLine("Rows Affected: " + rowsAffected);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
```
请注意,您需要使用与您的SQL Server实例匹配的正确连接字符串,并根据需要更改表名和列名。此外,使用参数化查询可以帮助防止SQL注入攻击。
阅读全文