1

私は数年間 VB.NET でプログラムを開発しており、VB.NET に精通しています。私があまり触れていない分野はデータベースです。

私は、ムービー マネージャーと呼ばれる (個人的な使用のための) プログラムを作成しています。私が持っている映画の情報を保存します。Sql Server Compact Edition データベースを選択しました。Info と Cast という 2 つのテーブルを持つデータベースがあるとします。Info テーブルには、movie_name、release_date などのいくつかの列があります。キャスト テーブルには、first_name、last_name などの列がほとんどありません。

現在、データベースからテーブルのすべての情報を読み取る DataSet を作成しました (接続を開き、テーブル情報を入力し、接続を閉じます)。このようにして、グローバル変数にデータベースのスナップショットがあります。

私のクエリ:

  1. データを取得したら、レコードを追加、編集、または削除する必要があるたびに、接続を開き、SQL を起動して、接続を閉じる必要があります。右 ?Sql を使用せずにこれを行う方法はありますか? さらに、このコンセプトは大丈夫です。

  2. 私は構造体を使用していないので、一時情報を保存するために空のデータセットを作成する必要があります。これは便利ですか?

  3. データセット テーブルで特定のものを検索する必要がある場合、すべてのアイテムをループする必要がありますか、またはデータセットで sql を使用できますか、それとも代替手段がありますか?

4

3 に答える 3

3

1)データを取得したら、レコードを追加、編集、または削除する必要があるたびに、接続を開き、SQL を起動して接続を閉じる必要があります。右 ?Sql を使用せずにこれを行う方法はありますか? さらに、このコンセプトは大丈夫です。

いいえ。データベースを更新するには、データベースを使用する必要があります。データベースにストアド プロシージャを作成して機能を処理し、それをコードから呼び出して、保存する必要があるデータを渡します。インライン SQL を使用しないでください。パラメータ化されたストアド プロシージャが最適です。

2) 構造体を使用していないため、一時情報を格納するために空のデータセットを作成する必要があります。これは便利ですか?

それはあなたが何をしているかによります。更新したデータを保持するオブジェクト モデルを作成し、変更を保存するときにプロパティをストアド プロシージャに渡します。

3) データセット テーブルで特定のものを検索する必要がある場合、すべてのアイテムをループする必要がありますか、またはデータセットで sql を使用できますか、それとも代替手段がありますか?

行をループするか、linq を使用して必要なものを引き出すことができます。Linq は、基本的にコレクションに対する .NET コード クエリであるため、非常に優れています。

コードからストアド プロシージャ コールを介して更新する方法を示すチュートリアル/ガイドがたくさんあります。linq チュートリアルもたくさんあります。基本的に、テーブルに対する linq クエリは次のようになります。

dim result as Generic.List(of String) =
  (from r in table.AsEnumerable()
  select r
  where r["columnName"] = "the value").ToList()

この構文はおそらく少しずれていますが、そのように見えます。

編集

あなたのモデル:

Public Class EmployeeModel
    Public Property Id
    Public Property FirstName
    Public Property Last Name
    Public Property JobCode
    Public Sub EmployeeModel(your params)
        //set properties
    End Sub
End Class

あなたの DAL:

Public Shared Class EmployeeDAL
    Public Shared Sub SaveEmployee(ByRef model as EmployeeModel)
       // call your sp_SaveEmployee stored procedure and set the parameters to the model properties
       // @id = EmpoyeeModel.Id
       // @JobCode = Employee.JobCode
       // ...
    End Sub
End Class

私は数か月ごとに VB を使用しているため、小さな構文エラーがいくつかある可能性があります。しかし、それは基本的にあなたがする必要があるすべてです。データを保存する関数は、クラス自体ではなく、DAL にあります。ただし、DAL を使用したくない場合は、クラスに保存機能を配置できます。同じように機能しますが、明確に分離されていません。

于 2013-04-28T05:16:56.513 に答える
2

あなたの質問について。

番号 1: データを保存および取得するには、データベースに接続する必要があります。それに対処する方法はたくさんありますが、その 1 つの方法は app.config を使用するか、必要なときに接続を呼び出す関数を単純に作成することです。

番号 2: ここではデータセットを扱っているため、 DataSetを参照することをお勧めします。

番号 3: Data Adapter と Data Table を使用して試すこともできます。質問番号 3 の意味がわかりません。

乾杯

于 2013-04-30T16:36:34.390 に答える
0

I have problem with the way you are using your database and the ram of your computer. Problem1: since you already have a database for holding the movies information why are you again holding the same information in memory?, creating an extra overhead. if your answer is for performance or i have cheap memory then why don't you use xml or flatfile instead? Database is not needed with this senario.

Problem2: You are like a soldier who dosent know about the weapon he use? right? because you are asking silly questions your first question about opening connection.. the answer is yes you have to open the connection every time save/read the data and close it as soon as possible. your second question about convinent the answer is no. instead create class with all field as property and some method for initialization,saving,deleting. this way you have to write less code. nad suppose you have a movie names xyz there can be another movie xyz how will you distinguish it? if you have whole information b4 you you can do it via release date ,casts etc, but still it will be hard, so create a primary key for both your table

and finally your 3rd question , it will be easier to use use sql queries than looping thru the dataset(get rid of the dataset as soon as possible) wish yu good luck on the road to rdbms

于 2013-04-28T04:13:38.033 に答える