如何使用Visual Studio和C#将数据插入并保存到数据库中?

时间:2017-04-17 10:27:20

标签: c# sql-server

public string ss = "Data Source=D\\SQLEXPRESS;Initial Catalog=gym;Integrated Security=True";

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    string q2 = "insert into gym.dbo.customer (name, weight, height, add_class, gender, fees) values ('" + this.textBox1.Text + "','" + this.textBox2.Text + "','" + this.textBox3.Text + "','" + this.comboBox1.Text + "','" + this.comboBox2.Text + "','" + this.comboBox3.Text + " ') ;";

    SqlConnection con = new SqlConnection(ss);
    SqlCommand cmd = new SqlCommand(q2, con);
    SqlDataReader read;

    try
    {
        con.Open();
        read = cmd.ExecuteReader();
        MessageBox.Show("Welcome to our gym");

        while (read.Read()) { };
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

如何使用Visual Studio和C#将数据插入数据库并将其保存到数据库中?

此代码抛出错误。任何人都可以向我提出解决错误的建议。

image description

5 个答案:

答案 0 :(得分:2)

首先确保您的客户表的不同列的数据类型。 然后确保您需要为组合框保存哪种类型的数据。

您必须从Combobox中获取所选值。 combobox1,combobox2,combobox3仅返回类名

 System.Windows.Forms.ComboBox

除了其他人,建议使用参数..像这样: 您可以按照此示例

private void button1_Click(object sender, EventArgs e)
{
    using(SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\abdul samad\documents\visual studio 2013\Projects\newpro\newpro\Database1.mdf;Integrated Security=True"))
    {
        try
        {

            using (var cmd = new SqlCommand("INSERT INTO registor (Name, FullName, Password, Email, Gander) VALUES (@Name,@Fullname,@Password,@Email, @Gander)"))
            {

                cmd.Connection = con;   
                cmd.Parameters.Add("@Name", txtfname.Text);
                cmd.Parameters.Add("@Fullname", txtfname.Text);
                cmd.Parameters.Add("@Password", txtpass.Text);
                cmd.Parameters.Add("@Email", txtemail.Text);
                cmd.Parameters.Add("@Gander", comboBox1.GetItemText(comboBox1.SelectedItem));

                con.Open()
                if(cmd.ExecuteNonQuery() > 0) 
                {
                   MessageBox.Show("Record inserted"); 
                }
                else
                {
                   MessageBox.Show("Record failed");
                }
            }
        }
        catch (Exception e)
        {
            MessageBox.Show("Error during insert: " + e.Message);
        }
    }
}

答案 1 :(得分:0)

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) 
            { 
                SqlCommand cmd = new SqlCommand("insert into customer (name,weight,height,add_class,gender,fees) values(@name,@weight,@height,@add_class,@gender,@fees)", con); 
                cmd.Parameters.AddWithValue("name", this.textBox1.Text); 
                if (con.State == ConnectionState.Closed) 
                con.Open(); 
                cmd.ExecuteNonQuery(); 
                con.Close(); 
            }

答案 2 :(得分:0)

评论有点忙,所以这是你需要做的事情(包括参数化查询):

具体来说,您不需要读者来插入声明,因为它不会返回结果集。

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    var sql = "insert into dbo.customer ...";
    using (var con = new SqlConnection(ss))
    {

        var cmd = new SqlCommand(sql , con);
        con.Open();
        cmd.ExecuteScalar();
        MessageBox.Show("Welcome to our gym");
    }
}

答案 3 :(得分:0)

您好检查健身房数据库中是否有客户表。 别试这个link

答案 4 :(得分:0)

我发现您的连接字符串声明错误

public string ss = "Data Source=D\\SQLEXPRESS;Initial Catalog=gym;Integrated Security=True";

需要像下面这样更新

public string ss = "Data Source=abc\\SQLEXPRESS;Initial Catalog=gym; user id=sa; Password=123456";

数据源不是D,应该是服务器名称。

enter image description here