【问题标题】:How to insert the row into the table of SQL database?如何将行插入到 SQL 数据库的表中?
【发布时间】:2021-03-15 08:10:09
【问题描述】:

我的问题看起来很琐碎,但不幸的是我不知道如何解决这个问题。

一个数据库中有一个表。我必须通过单击该表的按钮来添加数据。当用户单击按钮时,他/她将进入某种形式,他/她可以在其中输入数据。第一次最好能输入:id、服务种类、价格。我决定创建一个新类,我将在其中包含所有变量,包括这三个变量。这些变量是公开的。

我还决定从文本框中读取文本并将此信息写入该类的变量中。在第二种形式中,有 2 个按钮。 “确定”和“取消”。我决定使用 ShowDialog。

我能够将表从数据库输出到 DataGridView,但我不太清楚如何将数据添加到我的表中并在插入后在 datagridview 中成功展示。

我的班级:

 public class AllDataDB
{ 
    public int id_serv;
    public double price;
    public string name;
}

第二种形式:

public partial class TypeService : Form
{
    public AllDataDB Class;
    public TypeService(AllDataDB t)
    {
        Class = t;
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        AllDataDB Class = new AllDataDB();

        Class.id_serv = Convert.ToInt32(textBox3.Text);
        Class.name = NameService.Text;
        Class.price = Convert.ToDouble(PriceService.Text);

        this.DialogResult = DialogResult.OK;
        this.Close();
    }
}

按钮调用表单和查询的工作:

private void NewServe_Click(object sender, EventArgs e)
    { 

        AllDataDB Class = new AllDataDB();
        TypeService form = new TypeService(Class);

        if (form.ShowDialog() == DialogResult.OK)
        { // відповідно до класу створюється новий запис. INSERT.

                SqlConnection Con = new SqlConnection(connectionString);
                Con.Open();
                string Que = "INSERT INTO type_service " +
                        "VALUES(" + Class.id_serv + " ,'" + Class.name +
                        "' ," + Class.price + " );" +
                        "SELECT * FROM type_service";
                SqlCommand cmd = new SqlCommand(Que, Con);
                cmd.ExecuteNonQuery();
                Con.Close();

                SqlDataAdapter sqlDa = new SqlDataAdapter("SELECT * FROM type_service", Con);
                DataTable d = new DataTable(); 
                sqlDa.Fill(d);

                dataGridView3.AutoGenerateColumns = false;
                dataGridView3.DataSource = d;
        }
    }

【问题讨论】:

标签: c# sql sql-insert showdialog


【解决方案1】:

显示的代码有很多问题。最重要的是读取值的方式以及如何尝试插入数据。在 TypeService 表单中,您创建了一个 AllDataDB 类的新实例,用于存储结果。该实例不再是您在输入中传递的实例,因此您需要从 TypeService 表单创建的实例中读取值(存储在全局字段 Class 中)。第二个问题是值的字符串连接。这是一个众所周知的问题,会导致解析问题和 sql 注入。它通过参数化查询修复,如下所示

private void NewServe_Click(object sender, EventArgs e)
{ 
    // Do no pass an instance of Class here, just pass null 
    // or remove it at all if you don't plan to reuse the form for updating 
    TypeService form = new TypeService(null);
    if (form.ShowDialog() == DialogResult.OK)
    { 
        // Add the using statement to ensure a proper release of resources.
        using SqlConnection Con = new SqlConnection(connectionString);
        Con.Open();
        // Parameterized query
        string Que = "INSERT INTO type_service VALUES(@id,@name,@price);";
        SqlCommand cmd = new SqlCommand(Que, Con);
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = form.Class.id_serv;    
        cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = form.Class.name;
        cmd.Parameters.Add("@price", SqlDbType.Decimal).Value = form.Class.price;
        cmd.ExecuteNonQuery();

        SqlDataAdapter sqlDa = new SqlDataAdapter("SELECT * FROM type_service", Con);
        DataTable d = new DataTable(); 
        sqlDa.Fill(d);
        dataGridView3.AutoGenerateColumns = false;
        dataGridView3.DataSource = d;
    }
}

还有其他小问题。在 TypeService 表单和 AllDataDB 类中,您使用了全局字段,而不是更灵活的属性使用方式。

public class AllDataDB
{ 
    public int id_serv {get;set;}
    public double price {get;set;}
    public string name {get;set;}
}

还有

public AllDataDB Class {get;set;}

此外,鉴于传递给 TypeService 表单的构造函数的 null,您现在需要确保在不检查其 null 值的情况下不使用该 Class 属性

【讨论】:

  • 你能解释一下,我在哪里使用了“public AddDataDB Class {get; set;}”?
  • 替换TypeService类里面的行
  • 我的问题是未处理的异常。
  • "我的问题是未处理的异常" 不,您的问题不仅是没有处理异常,还必须修复导致这些异常的原因。
【解决方案2】:

嗯,这个帮助非常有用。谢谢。但是我已经通过在 TypeService 类中创建一个新的 3 个变量来解决这个问题。这些变量也是公开的。看起来像这样:

在 TypeService 类中:

public int i;
public string n;
public double p;

在 MainCore 类中:

cmd.Parameters.Add("@id", SqlDbType.Int).Value = form.i;// form.Class.id_serv;
cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = form.n;//form.Class.name;
cmd.Parameters.Add("@price", SqlDbType.Decimal).Value = form.p; //form.Class.price;

所以它起作用了。而且我可以删除类 AllDataDB,因为它现在不重要和有用。 感谢您的帮助。

【讨论】:

    猜你喜欢
    • 2012-02-22
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多