0

Visual Studioでクラスにストアドプロシージャを呼び出しますデータを検索し、最後にsp_viewというSQLサーバにデータを表示すると、enter image description here のようになります。私はストアドプロシージャをテストし、それは正常に実行されました。今私は既にデータベースに接続しているVs2015に私はそれが成功したテスト接続をクリックするまで今すぐ接続されたUkをクリックします。今、私たちは私のVS2015のC#でWindowsフォームのUI上にある私は、単純なストアドプロシージャにそのデータを削除します<strong>sp_delete</strong>データを更新してしまうデータと<strong>sp_Update</strong>を挿入する<strong>sp_insert</strong>を書き、<strong>sp_search</strong>その

enter image description here

。これらのように見えます。私はクラスを作成しました私は私のプロジェクトのソリューションエクスプローラでリポジトリという名前を付けました。これらのクラスはここにコードがあります ここにあります。私の問題は私は4つのストアドプロシージャをsp_insert、sp_Update、sp_delete 、sp_search、私のクラスのsp_viewとshow.isには何も私のコードに関する問題はありません。

public class repositories 
    { 
     //Initializes a new instance of the DataContext class by referencing  the connection name of database. 
    public static DataClasses1DataContext db = null; 

    //that registration is my table 
    public static void add(registration) 
    { 
     db = new DataClasses1DataContext(); 
      //they are getting red cannot call sp_insert that i was created in sql server in my whole entire procedure. 
     db.sp_insert(Username, password, securityQuestion, SecurityAnswer, LastName, firstName, MiddleInitial, address, gender, birthdate, age); 
    } 
    public static void Update (registration) 
    { 
     db = new DataClasses1DataContext(); 
     db.sp_Update(Username, password, securityQuestion, SecurityAnswer, LastName, firstName, MiddleInitial, address, gender, birthdate, age); 
    } 
    public static void delete (registration) 
    { 
     db = new DataClasses1DataContext(); 
     db.sp_Delete(UserId); 
    } 
    public static List<sp_ViewResult> View() 
    { 
     db = new DataClasses1DataContext(); 
     List<sp_ViewResult> View() = db.sp_view().Tolist<sp_viewResult>(); 
    } 


} 

} 

私はそうする際にいくつかのガイダンスが必要です。誰でも私を助けることができます..

あなたの助けに感謝します。

+3

Proの先端を使用する:(https://msdn.microsoft.com/en-us/library/dd172115(V = VS.100).aspxの[ 'sp_'接頭辞を使用しないでください]を)。 –

+0

'DataClasses1DataContext'のコードはどこから来たのですか?あなたはそれを書きましたか?それはEFから生成されましたか? –

+0

@Jhon Wuはコメントをお寄せいただきありがとうございますが、なぜsp_がストアドプロシージャとしてSQL Serverから作成されたのですか? – Sharkweb

答えて

0

エンティティフレームワークを使用しており、登録が問題ではないオブジェクトである場合、このコードは機能するはずです。

また、ストアドプロシージャをエンティティフレームワークモデルに追加したことを確認してください。追加された場合は、クラス全体ではなく値を追加しているため、エラーが発生する可能性があります。

FYI。デバッグモードを実行している場合は、 のプロセス中にエラーが発生した場合はキャッチする必要がありますが、リリースを実行するとエラーがキャッチされません。

システムを使用してインポートする必要があります。 IDisposableを

public class repositories: IDisposable 
{ 
    private DataClasses1DataContext _db; 
    public DataClasses1DataContext db 
    { 
     get 
     { 
      if (_db == null) 
      { 
       _db = new DataClasses1DataContext(); 
      } 

      return _db; 
     } 
    } 

    public static void add(registration reg) 
    { 
     db.sp_insert(reg.Username, reg.password, reg.securityQuestion, reg.SecurityAnswer, reg.LastName, reg.firstName, reg.MiddleInitial, reg.address, reg.gender, reg.birthdate, reg.age); 
    } 
    public static void Update(registration reg) 
    { 
     db.sp_Update(reg.Username, reg.password, reg.securityQuestion, reg.SecurityAnswer, reg.LastName, reg.firstName, reg.MiddleInitial, reg.address, reg.gender, reg.birthdate, reg.age); 
    } 
    public static void delete(registration reg) 
    { 
     db.sp_Delete(reg.UserId); 
    } 
    public static List<sp_ViewResult> View() 
    { 
     List<sp_ViewResult> View() = db.sp_view().Tolist<sp_viewResult>(); 
    } 

    public void Dispose() 
    { 
     db.Dispose(); 
    } 
} 
+0

ありがとうございます。登録時にregを追加すると今すぐ呼び出すことができます – Sharkweb

+0

このプライベートなDataClasses1DataContext _dbのようになるのはどのような用途ですか? public DataClasses1DataContext db – Sharkweb

+0

フィールドは実際には動作しますが、すべてdbです。 on public static void add、update、delete、viewにアクセスできなかったか、エラー – Sharkweb

関連する問題

 関連する問題