首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据库与SQL C#

数据库与SQL C#
EN

Stack Overflow用户
提问于 2012-04-20 10:01:11
回答 2查看 210关注 0票数 0

我知道我以前也问过一个类似的问题,但我还是无法做到这一点。

我在VS2010中使用VS2010

我正在做的是创建一个应用程序,让用户在textbox1中输入单词,单击一个按钮,并将前面有"*"的所有单词打印在textbox2

我使用Server数据库进行此操作,因为我希望将"*"单词存储在数据库中,以便稍后可以添加一个计数器来显示输入该单词的次数。

例如:

textbox2

  • 用户在textbox1中输入the cat is *brown,按下按钮,然后*brown出现在textbox1中。

我为Server编写的代码如下:

代码语言:javascript
复制
SqlConnection con = new SqlConnection(@"Server=.\SQLEXPRESS;Database=StoreList;Integrated Security=sspi");

con.Open();
String queryStr = "SELECT item FROM StoreList WHERE item LIKE '*%'";
SqlCommand com = new SqlCommand(queryStr, con);
SqlDataReader sdr = com.ExecuteReader();

while (sdr.Read())
{
   this.textbox2.Text = sdr.GetValue(0).ToString();
}

sdr.Close();

我需要帮助的是如何进一步的代码,以使应用程序工作。所以我不知道如何让它从textbox1那里得到消息。

有帮手吗?

EN

回答 2

Stack Overflow用户

发布于 2012-04-20 10:21:24

忽略您的代码示例,在我看来,这里发生了一些事情。

  1. 您想要构建一个输入到textbox1
  2. 中的星号单词列表,您想要将明星的单词列表输出到textbox2
  3. Store中,这是一个被盯着的单词

的频率列表。

第1点和第2点可以在不与数据库交互的情况下完成。也许使用正则表达式来匹配与*匹配的单词。

第3点应该是执行sql语句的情况,该语句要么插入带有计数为1的新单词,要么将已知单词的计数更新为1。

编辑以下注释

第1点-建立一个单词列表

代码语言:javascript
复制
var matches = new System.Text.RegularExpressions.Regex("\\*[^ \t]+").Matches(textbox1);

foreach (Match match in matches)
    Console.WriteLine(match.Value);

第3点-为频率列表存储单词

代码语言:javascript
复制
//using statement to ensure connection is cleaned up correctly
using (SqlConnection connection = new SqlConnection(@"Server=.\SQLEXPRESS;Database=StoreList;Integrated Security=sspi"))
{
    connection.Open();

    foreach (Match match in matches)
    {
        var command = new SqlCommand("UpdateWordFrequency", connection);

        command.CommandType = System.Data.CommandType.StoredProcedure;
        command.Parameters.Add(new SqlParameter("@word", match.Value));

        command.ExecuteNonQuery();
    }
}

在那里手术看起来像这样

代码语言:javascript
复制
create procedure UpdateWordFrequency
    @word varchar(50)
as
if not exists (select 1 from WordList where word = @word)
    insert into WordList (word, frequency) values (@word, 0)

update WordList set frequency = frequency + 1 where word = @word

WordList表包含两个字段:word (varchar)和frequency (int)。

第2点-输出星字

这应该是从WordList表中选择的一种情况

票数 2
EN

Stack Overflow用户

发布于 2012-04-20 10:14:56

为此使用存储过程可能更好。

通过使用存储过程,您可以执行如下操作:

代码语言:javascript
复制
create procedure Insert_StoreList
@Word nvarchar(50)
AS

insert StoreList (
item )
values (
@Word )

SELECT item FROM StoreList WHERE item LIKE '*%'

然后,您可以在C#中使用以下内容

代码语言:javascript
复制
SqlCommand com = new SqlCommand("Insert_StoreList", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add(new SqlParameter("@Word", Textbox1.Text));
SqlDataReader sdr = com.ExecuteReader();

编辑--如果您不喜欢使用存储过程,您也可以使用2行SQL来实现这一点,第一行是将值输入表的ExecuteNonQuery,第二行是从表中选择所有单词。

第一个sql是:

代码语言:javascript
复制
String queryStr = "insert StoreList (item) values (" + Textbox1.Text + ")"; 

第二个sql是:

代码语言:javascript
复制
String queryStr = "SELECT item FROM StoreList WHERE item LIKE '*%'";
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10244057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档