3

当事件Button被按下时,SQL 表中没有任何更新并且没有错误显示。

protected void SubmitBTN_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Matt\Documents\coffeeShop.mdf;Integrated Security=True;Connect Timeout=30");

    String coffeeName = NameTXT.Text;
    String coffeeGrid = GrindTXT.Text;
    String coffeeOrigin = OriginTXT.Text;
    String coffeePrice = PriceTXT.Text;
    String coffeeQty = QuantityTXT.Text;
    String coffeeRRP = RRPTXT.Text;

    SqlCommand comm = new SqlCommand("INSERT INTO Table (coffeeName, coffeeGrid, coffeeOrigin, coffeePrice, coffeeQty, coffeeRRP) VALUES ('%" + coffeeName + "%','%" + coffeeGrid + "%','%" + coffeeOrigin + "%','%" + coffeePrice + "%','%" + coffeeGrid + "%','%" + coffeeQty + "%','%" + coffeeRRP + "%' ", conn);

    conn.Open();
    //SqlDataReader reader = comm.ExecuteReader();

    //lblDBData.Text += "<table border=0>";
    //while (reader.Read())
    //{
    //    lblDBData.Text += "<tr>";
    //    lblDBData.Text += "<td>" + reader["coffeeName"] + "</td>";
    //    lblDBData.Text += "</tr>";
    //}
    //lblDBData.Text += "</table>";

    //reader.Close();
    conn.Close();                     
}

任何建议将不胜感激,非常感谢

4

5 回答 5

4

添加:

comm.ExecuteNonQuery();

后:

conn.Open();

顺便说一句,您可能希望使用参数而不是" + parameter + " 查询来避免 sql 注入。读这个:

http://www.csharp-station.com/Tutorial/AdoDotNet/Lesson06

于 2013-02-26T14:28:33.200 回答
2

您需要执行命令为;

conn.Open(); //Open the connection to the database
comm.ExecuteNonQuery(); //This line does the insert
conn.Close(); //Close the connection once your command executed.

还要考虑参数化查询并在using块内打开连接对象作为避免打开连接对象的好习惯。

前任;

using(SqlConnection conn = new SqlConnection("connectionString"))
{
   SqlCommand cmd = new SqlCommand("your query string with @para", conn);
   cmd.Parameters.AddWithValue("@para", "value");
   conn.Open();
   cmd.ExecuteNonQuery();

}
于 2013-02-26T14:28:00.087 回答
1

执行 Transact-SQL 语句时,正确的方法是:

    private const string connection = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Matt\Documents\coffeeShop.mdf;Integrated Security=True;Connect Timeout=30";

    protected void SubmitBTN_Click(object sender, EventArgs e)
    {
        string query = "INSERT INTO Table (coffeeName, coffeeGrid, coffeeOrigin, coffeePrice, coffeeQty, coffeeRRP) VALUES (@name, @grid, @origin, @price, @qty, @rrp)";
        using(SqlConnection conn = new SqlConnection(connection))
        using(SqlCommand command = new SqlCommand(query, connection))
        {        

            String coffeeName = NameTXT.Text;
            String coffeeGrid = GrindTXT.Text;
            String coffeeOrigin = OriginTXT.Text;
            String coffeePrice = PriceTXT.Text;
            String coffeeQty = QuantityTXT.Text;
            String coffeeRRP = RRPTXT.Text;

            command.Parameters.AddWithValue("@name", coffeeName);
            command.Parameters.AddWithValue("@grid", coffeeGrid);
            command.Parameters.AddWithValue("@origin", coffeeOrigin);
            command.Parameters.AddWithValue("@price", coffeePrice);
            command.Parameters.AddWithValue("@qty", coffeeQty);
            command.Parameters.AddWithValue("@rrp", coffeeRRP);

            try
            {
                command.Connection.Open();
                command.ExecuteNonQuery();
            }
            catch (SqlException Ex)
            {

                console.WriteLine( "Error message: " + Ex);
            }
            finally
            {
                command.Connection.Close();
            }        

        }

    }
于 2019-08-30T09:08:30.500 回答
0

您需要执行 SQL 命令。在关闭连接之前,添加以下内容:

comm.ExecuteNonQuery();

例如,请参阅 MSDN:http: //msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx

于 2013-02-26T14:29:25.487 回答
0

您无法阅读插入语句。您必须使用comm.executeNonQuery()执行插入命令,然后制作一个新的选择语句来读取数据

于 2013-02-26T14:27:53.800 回答