0

私の接続文字列は次のようになっていました:

SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=G:\I.S\C#\billingSystem\Store.mdf;Integrated Security=True;User Instance=True");

だから私はこれで(挿入、更新、削除)することができます:

    SqlCommand cmd = new SqlCommand("SQLSTATEMENT", cn);
    cn.Open();
    cmd.ExecuteNonQuery();
    cn.Close();

彼が使用したとき、「Deploy C# Project」と呼ばれるYouTubeのチュートリアルを見ました

System.Configuration;

app.configファイルなので、チュートリアルをまったく同じ方法で実行したところ、機能しました。

実行時に (挿入、更新、削除) した後、アプリケーションを閉じるまではすべて問題ありません。

実行時に変更をテーブルに保存する必要があります。

いくつかの情報: Winforms アプリ、私のコードからのサンプル:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="SchoolProjectConnectionString"
            connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\SchoolDB.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

namespace SchoolProject
{
    public partial class Main : Form
    {
        SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SchoolProjectConnectionString"].ToString());
        public Main()
        {
            InitializeComponent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string myValue = Interaction.InputBox("Enter ID", "ID", "", 100, 100);
            string myValue0 = Interaction.InputBox("Enter Name", "Name", "", 100, 100);
            int X = Convert.ToInt32(myValue);

            SqlCommand cmd = new SqlCommand("Insert into Student(ID, Student) Values ('" + X + "','" + myValue0 + "')", cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string myValue3 = Interaction.InputBox("Enter ID", "ID", "", 100, 100);
            int X = Convert.ToInt32(myValue3);

            SqlCommand cmd = new SqlCommand("SELECT * FROM Student WHERE id = '" + X + "'", cn);
            cn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    MessageBox.Show(reader["Student"].ToString());
                }
            }
            cn.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Report R = new Report();
            R.Show();
        }
    }
}
4

1 に答える 1

2

User Instance と AttachDbFileName=のアプローチ全体に欠陥があります - せいぜい! .mdfVisual Studio でアプリを実行すると、ファイルが (App_Dataディレクトリから出力ディレクトリ (通常.\bin\debugはアプリが実行される場所) に) コピーされ、ほとんどの場合、問題なく動作しますが、間違ったINSERTものを見ているだけです。最後にmdfファイル

このアプローチに固執したい場合は、myConnection.Close()呼び出しにブレークポイントを設定してから、 .mdfSQL Server Mgmt Studio Express でファイルを調べてください。データがそこにあることはほぼ確実です。

私の意見では、本当の解決策は

  1. SQL Server Express をインストールします (とにかく、既に完了しています)。

  2. SQL Server Management Studio Express をインストールする

  3. SSMS Expressでデータベースを作成し、論理名を付けます (例: Store)

  4. 論理データベース名(サーバー上でデータベースを作成したときに指定) を使用して接続し、物理データベース ファイルとユーザー インスタンスをいじらないでください。その場合、接続文字列は次のようになります。

    Data Source=.\\SQLEXPRESS;Database=Store;Integrated Security=True
    

    そして、それ以外はすべて以前とまったく同じです...

また、SQL インジェクションについて調べ、それを回避するようにコードを変更する必要があります。これは、アプリにとって最大のセキュリティ リスクです。代わりに、ADO.NET でパラメーター化されたクエリを使用する必要があります。これらは安全であり、高速でもあります。

于 2013-07-07T11:45:03.480 に答える