我知道我以前也问过一个类似的问题,但我还是无法做到这一点。
我在VS2010中使用VS2010
我正在做的是创建一个应用程序,让用户在textbox1中输入单词,单击一个按钮,并将前面有"*"的所有单词打印在textbox2中
我使用Server数据库进行此操作,因为我希望将"*"单词存储在数据库中,以便稍后可以添加一个计数器来显示输入该单词的次数。
例如:
textbox2
textbox1中输入the cat is *brown,按下按钮,然后*brown出现在textbox1中。我为Server编写的代码如下:
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那里得到消息。
有帮手吗?
发布于 2012-04-20 10:21:24
忽略您的代码示例,在我看来,这里发生了一些事情。
的频率列表。
第1点和第2点可以在不与数据库交互的情况下完成。也许使用正则表达式来匹配与*匹配的单词。
第3点应该是执行sql语句的情况,该语句要么插入带有计数为1的新单词,要么将已知单词的计数更新为1。
编辑以下注释
第1点-建立一个单词列表
var matches = new System.Text.RegularExpressions.Regex("\\*[^ \t]+").Matches(textbox1);
foreach (Match match in matches)
Console.WriteLine(match.Value);第3点-为频率列表存储单词
//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();
}
}在那里手术看起来像这样
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 = @wordWordList表包含两个字段:word (varchar)和frequency (int)。
第2点-输出星字
这应该是从WordList表中选择的一种情况
发布于 2012-04-20 10:14:56
为此使用存储过程可能更好。
通过使用存储过程,您可以执行如下操作:
create procedure Insert_StoreList
@Word nvarchar(50)
AS
insert StoreList (
item )
values (
@Word )
SELECT item FROM StoreList WHERE item LIKE '*%'然后,您可以在C#中使用以下内容
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是:
String queryStr = "insert StoreList (item) values (" + Textbox1.Text + ")"; 第二个sql是:
String queryStr = "SELECT item FROM StoreList WHERE item LIKE '*%'";https://stackoverflow.com/questions/10244057
复制相似问题