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

挿入ステートメントを読み取ることはできません。を使用comm.executeNonQuery()して挿入コマンドを実行してから、新しいselectステートメントを作成してデータを読み取る必要があります

于 2013-02-26T14:27:53.800 に答える
0

SQLコマンドを実行する必要があります。接続を閉じる前に、次を追加します。

comm.ExecuteNonQuery();

例については、MSDNを参照してください:http: //msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx

于 2013-02-26T14:29:25.487 に答える