简体   繁体   English

C# 中的 SqlDataReader 和 SqlCommand

[英]SqlDataReader and SqlCommand in C#

private void button1_Click(object sender, EventArgs e)
{
    object functionReturnValue = null;

    using (System.Net.WebClient client = new System.Net.WebClient())
    {
        System.Collections.Specialized.NameValueCollection parameter = new System.Collections.Specialized.NameValueCollection();
        string url = "https://www.itexmo.com/php_api/api.php";

        conn.Open();
        cmd = new SqlCommand("select  FirstYear from Numbers", conn);

        dr = cmd.ExecuteReader();
           
        parameter.Add("1", "09123456789");
        parameter.Add("2", richTextBox1.Text);
        parameter.Add("3", "TR-CCDIS629375_B9R26");
        parameter.Add("passwd", "u$454g4gi&w&");

        dynamic rpb = client.UploadValues(url, "POST", parameter);

        functionReturnValue = (new System.Text.UTF8Encoding()).GetString(rpb);
        MessageBox.Show("Message has been sent!.", "Message", MessageBoxButtons.OK);
        dr.Close();
        conn.Close();
    }
}

This code here is a normal code to send sms using an API to a single number, I only added the SqlCommand to it and I know what to do about it, how am I going to get the multiple numbers from my database which I want to access using a comboBox?这段代码是使用 API 向单个号码发送短信的普通代码,我只向其中添加了SqlCommand ,我知道该怎么做,我如何从我的数据库中获取我想要的多个号码使用 comboBox 访问? As you can see in my cmd SqlCommand I'm trying to retrieve the data but I don't know to how to convert into a parameter to replace the single number in the parameter and send sms to multiple numbers.正如您在我的cmd SqlCommand中看到的那样,我正在尝试检索数据,但我不知道如何转换为参数以替换参数中的单个数字并将短信发送到多个数字。 I am new to C#, any help is much appreciated.我是 C# 的新手,非常感谢任何帮助。

how am i going to get the multiple numbers from my database which i want to access using a comboBox?我如何从我的数据库中获取我想使用 comboBox 访问的多个号码?

You could do this in eg the constructor of the form after InitializeComponent() , or some other button that is a "load numbers from db" button:您可以在InitializeComponent()之后的表单构造函数中执行此操作,或者在其他一些按钮中执行此操作,该按钮是“从数据库加载数字”按钮:

var dt = new DataTable();
using var da = new SqlDataAdapter("select  FirstYear from Numbers", conn); //perhaps use the overload that takes a connectionstring instead; avoid having SqlConnections in class level variables
da.Fill(dt);
_numbersComboBox.DisplayMember = "FirstYear";
_numbersComboBox.ValueMember = "FirstYear";
_numbersComboBox.DataSource = dt;

Then in your SMS send routine:然后在您的 SMS 发送例程中:

parameter.Add("2", _numbersComboBox.SelectedValue.ToString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM