asp.net批量添加手机号码,要求不重复

jadedm 2009-07-30 03:14:39
TextBox 只是添加手机号码的多行文本框 lable 是用户的登录名字 (固定)

目的 就是批量添加 手机号的 就是不让添加重复的 也不能和数据库中的 重复 用的是for循环批量添加的

下面的有错误 哪位帮看下 改下 最好有详细的 代码


protected void But_add_Click(object sender, EventArgs e)
{
string[] strArray = this.TextBox1.Text.ToString().Split('\n');
System.Collections.Hashtable strTable = new System.Collections.Hashtable();
for (int i = 0; i < strArray.Length; i++)
{
if (strArray[i].Length == 11)
{
if (!strTable.ContainsKey(strArray[i]))
{
strTable.Add(strArray[i], strArray[i]);
}
}
}
for (int i = 0; i < strTable.Count; i++)
{
string telNumber = strTable[i].ToString();// 未将对象引用到实例 ????

string strcon = @"Provider=Microsoft.Jet.OLEDB.4.0;data source=" + Server.MapPath("sms.mdb");
OleDbConnection conn = new OleDbConnection(strcon);
string sql = "INSERT INTO gusetinfo (username,guesttel) values('" + Label1.Text + "','" + telNumber + "')";
OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
cmd.ExecuteNonQuery();

}
...全文
334 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackyuen1 2009-08-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 dazhong23 的回复:]
你这根本就不对,数据库中重复的你没处理,先把数据从数据库取出来放到一个
Dictionary <string, string> Paras = new Dictionary <string, string>();
        if (!Paras.ContainsKey("name"))
        {
            Paras.Add("name", "value");
        }

之后再写回数据库。
你也可以把数据里没有直接插入数据库。
[/Quote]
正解
myzvszidone10 2009-08-01
  • 打赏
  • 举报
回复
string[] strArray = this.TextBox1.Text.ToString().Split(new char[]{'\n'});
Split()的参数类型……,如果是分割哪里的问题
jadedm 2009-08-01
  • 打赏
  • 举报
回复
我的就是一个号码放一行的
  • 打赏
  • 举报
回复
每个号码放一行


protected void btn_Click(object sender, EventArgs e)
{
string strTelphone = telphone.Text.Replace("\r\n",";");
if (strTelphone.EndsWith(";"))
{
strTelphone = strTelphone.Remove(strTelphone.Length - 1, 1);
}
string[] arrTelphone = strTelphone.Split(';');
System.Collections.Generic.HashSet<string> values = new HashSet<string>();
foreach (string item in arrTelphone)
{
if (values.Contains(item))
{
continue;
}
values.Add(item);
}
gv.DataSource = values;
gv.DataBind();
}

jadedm 2009-08-01
  • 打赏
  • 举报
回复
仔细研究下 。。。
wuyq11 2009-07-30
  • 打赏
  • 举报
回复
string[] content = textbox1.Text.Replace("\r", "").Split("\n");});
wuyq11 2009-07-30
  • 打赏
  • 举报
回复
Hashtable hashTable = new Hashtable();


foreach (DictionaryEntry de in hashTable)
{
System.Windows.Forms.MessageBox.Show(de.Key.ToString());
System.Windows.Forms.MessageBox.Show(de.Value.ToString());
}

System.Collections.IDictionaryEnumerator enumerator = hashTable.GetEnumerator();
while (enumerator.MoveNext())
{
System.Windows.Forms.MessageBox.Show(enumerator.Key.ToString());
System.Windows.Forms.MessageBox.Show( enumerator.Value.ToString());
}

GTBGT525 2009-07-30
  • 打赏
  • 举报
回复
  if (!strTable.ContainsKey(strArray[i])) 
{
strTable.Add(strArray[i], strArray[i]);
}


这你里已经对重复进行了处理。 只要把分割搞一搞就OK 了
GTBGT525 2009-07-30
  • 打赏
  • 举报
回复
protected void But_add_Click(object sender, EventArgs e) 
{
this.TextBox1.Text="13264598478,"
+"13264598578,"
+"13264598578,"
+"13264598538,"
+"13264598598,"
+"13264598518," ;

string[] strArray = this.TextBox1.Text.ToString().Split(",");
//请不要分割'\n' 换成","也行,其它符号也可以 我这里使用",";
System.Collections.Hashtable strTable = new System.Collections.Hashtable();
for (int i = 0; i < strArray.Length; i++)
{
if (strArray[i].Length == 11)
{
if (!strTable.ContainsKey(strArray[i]))
{
strTable.Add(strArray[i], strArray[i]);
}
}
}
for (int i = 0; i < strTable.Count; i++)
{
string telNumber = strTable[i].ToString();// 未将对象引用到实例 ????

string strcon = @"Provider=Microsoft.Jet.OLEDB.4.0;data source=" + Server.MapPath("sms.mdb");
OleDbConnection conn = new OleDbConnection(strcon);
string sql = "INSERT INTO gusetinfo (username,guesttel) values('" + Label1.Text + "','" + telNumber + "')";
OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
cmd.ExecuteNonQuery();

}
jadedm 2009-07-30
  • 打赏
  • 举报
回复
顶一下
jadedm 2009-07-30
  • 打赏
  • 举报
回复
哪位根据需求 给个完整的 楼上说的也是 数据库重复的没处理
dazhong23 2009-07-30
  • 打赏
  • 举报
回复
你这根本就不对,数据库中重复的你没处理,先把数据从数据库取出来放到一个
Dictionary<string, string> Paras = new Dictionary<string, string>();
if (!Paras.ContainsKey("name"))
{
Paras.Add("name", "value");
}

之后再写回数据库。
你也可以把数据里没有直接插入数据库。
jadedm 2009-07-30
  • 打赏
  • 举报
回复
是的 应该是没有分割成功
GTBGT525 2009-07-30
  • 打赏
  • 举报
回复
string[] strArray = this.TextBox1.Text.ToString().Split('\n');
//这里应该没有数据。分割不成功,试试strArray有没有数据 0吧我想
GTBGT525 2009-07-30
  • 打赏
  • 举报
回复


protected void But_add_Click(object sender, EventArgs e)
{
string[] strArray = this.TextBox1.Text.ToString().Split('\n');//这里应该没有数据。分割不成功,试试strArray有没有数据 0吧我想 System.Collections.Hashtable strTable = new System.Collections.Hashtable();
for (int i = 0; i < strArray.Length; i++)
{
if (strArray[i].Length == 11)
{
if (!strTable.ContainsKey(strArray[i]))
{
strTable.Add(strArray[i], strArray[i]);
}
}
}
for (int i = 0; i < strTable.Count; i++)
{
string telNumber = strTable[i].ToString();// 未将对象引用到实例 ????

string strcon = @"Provider=Microsoft.Jet.OLEDB.4.0;data source=" + Server.MapPath("sms.mdb");
OleDbConnection conn = new OleDbConnection(strcon);
string sql = "INSERT INTO gusetinfo (username,guesttel) values('" + Label1.Text + "','" + telNumber + "')";
OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
cmd.ExecuteNonQuery();

}


zzxap 2009-07-30
  • 打赏
  • 举报
回复
System.Collections.Hashtable strTable = new System.Collections.Hashtable();
放在外面
  • 打赏
  • 举报
回复
Hashtable 不是像你那样用的

请这样访问Hashtable 里的元素

foreach (DictionaryEntry objDE in telNumber )
{
string telKey = objDE.Key.ToString()
string telNumber = objDE.Value.ToString()
}





slimboy123 2009-07-30
  • 打赏
  • 举报
回复

using System.Collections;


foreach(DictionaryEntry de in strTable)
{
string telNumber = de.Value.ToString();
string strcon = @"Provider=Microsoft.Jet.OLEDB.4.0;data source=" + Server.MapPath("sms.mdb");

.....
.....
}

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧