【问题标题】:How to insert a string into an sql server table via a windows form? [closed]如何通过 windows 窗体将字符串插入到 sql server 表中? [关闭]
【发布时间】:2013-05-13 11:10:11
【问题描述】:

我的网站(远程)上有一个 sql server 表。该表名为table1,它有一个名为f1 的字段。我的目标是使用 C# 方法将字符串“hello there”插入其中,签名如下:

AddTof1(string s) 
{

}

有什么想法吗?

【问题讨论】:

  • 到目前为止你有没有尝试过?首先展示你的努力,这样人们才能展示他们的努力。请阅读FAQHow to Ask
  • 如果您想让回答者也这样做,您需要在您的问题上投入更多的时间和精力。

标签: c# sql-server winforms


【解决方案1】:
AddTof1(string s) 
{    
  using (SqlConnection connection = new SqlConnection(connectionString))
  {
      connection.Open();

      using (SqlCommand command = new SqlCommand("INSERT INTO table1 values(@s)", connection))
      {
          command.Parameters.AddWithValue("@s", s);
          command.ExecuteNonQuery();
      }
  }

}

那么你可以把这个方法称为;

AddTof1("hello there");

【讨论】:

  • @Downvoter 请发表评论,让我看看哪里有问题?
  • 谢谢,我不需要登录位,因为这是在我程序的其他地方完成的。 “@”字符有什么作用?需要吗?
  • @John @ 字符用于匹配查询中的参数。在这种情况下,您的查询中的 @s 参数与您的方法中的参数中的 s 变量匹配。
  • @John 谢谢。我认为你被否决了,因为你弯腰回答我的菜鸟问题哈哈。无论如何,谢谢。
猜你喜欢
  • 2015-02-22
  • 1970-01-01
  • 2016-06-24
  • 1970-01-01
  • 1970-01-01
  • 2016-03-31
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
相关资源
最近更新 更多