【问题标题】:Retrieve phone number from SQL Server database into Label Asp.net C# [closed]从 SQL Server 数据库中检索电话号码到 Label Asp.net C# [关闭]
【发布时间】:2015-04-14 08:06:37
【问题描述】:

我遇到了一个问题,该代码的 标签 需要存储从 SQL Server 数据库获取的电话号码。我需要使用该号码发送消息。

(我认为拨号代码中的+号有问题)

SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString);
connection.Open();           


SqlCommand com1 = new SqlCommand("select * from DB where Phone=@phone", connection);
com1.Parameters.AddWithValue("@phone", Label.Text);


string phone = Label1.Text;
Label1.Visible = true;

string AccountSid = "MyCode";
string AuthToken = "MYCODE";

var message = new TwilioRestClient(AccountSid, AuthToken);
var sms = message.SendMessage("MyNumber", phone, "Message Sent.", "");
Console.WriteLine(sms.Sid);

【问题讨论】:

  • 到底是什么问题?
  • 我不明白。你的问题到底是什么?而且您不会在代码中的任何地方使用com1
  • @SonerGönül 不,我已经使用 com1 来 addWithValue 参数语句。但是,我的问题是:如何从数据库 (SQL) 中获取数字并将该数字存储在 Label 中以在我的 C# 代码中使用它。
  • @AviGinsburg 不,我已经使用 com1 来 addWithValue 参数语句。但是,我的问题是:如何从数据库 (SQL) 中获取数字并将该数字存储在 Label 中以在我的 C# 代码中使用它。
  • SqlDataReader dr = com1.ExecucteDataReader();

标签: c# asp.net sql-server sms twilio


【解决方案1】:

您需要在代码中添加数据读取器。如下所示。

SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString);
connection.Open();           

string phone = Label1.Text;
Label1.Visible = true;

SqlCommand com1 = new SqlCommand("select * from DB where Phone=@phone", connection);
com1.Parameters.AddWithValue("@phone", Label.Text);
SqlDataReader reader = com1.ExecuteReader();

if (reader.HasRows()) 
{
   while (reader.Read())
   {
      phone = reader["ColumnName"].toString();
   }
}   

string AccountSid = "MyCode";
string AuthToken = "MYCODE";

var message = new TwilioRestClient(AccountSid, AuthToken);
var sms = message.SendMessage("MyNumber", phone, "Message Sent.", "");
Console.WriteLine(sms.Sid);

【讨论】:

    【解决方案2】:

    这是一个插入数据库的示例

    using System;
    using System.Data.SqlClient;
    using System.Transactions;
    
    namespace UnitTestProject2
    {
        public class User {
            public String Username {get;set;}
            public string Phone { get; set; }
        }
    
        public interface IRepository
        {
            void AddUser(User user);
            bool IsUsernamefree(User user);
        }
    
        public class RepositorySql : IRepository
        {
            private string constr;
            public RepositorySql(string connstr)
            {
                this.constr = connstr;
    
            }
            public void AddUser(User user)
            {
                using (SqlConnection conn = new SqlConnection(constr))
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand("insert into phoneNumbers (phone) values (@phone)", conn))
                    {
                        cmd.Parameters.AddWithValue("@phone", user.Phone);
                        cmd.ExecuteNonQuery();
                    }
                }
            }
        }
    
        public class UserRegistrationViewModel
        {
            private IRepository rep;
    
            public string ErrorMessage {get;set;}
    
            public UserRegistrationViewModel(IRepository rep)
            {
                this.rep = rep;
            }
    
            public bool AddUser(User user)
            {
                using (TransactionScope trans = new TransactionScope())
                {
                    rep.AddUser(user);
                    ErrorMessage = "";
                    return true;
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      • 2011-06-18
      • 1970-01-01
      相关资源
      最近更新 更多