简体   繁体   English

保存到DATABASE MVC模式

[英]Saving to DATABASE MVC pattern

I am fairly new to programming so please bear with me. 我是编程新手,请耐心等待。 I have written following code in different classes throughout my project, and I am wondering why my input does not get stored in my database. 我在整个项目的不同类中编写了以下代码,我想知道为什么我的输入没有存储在数据库中。 I really hope someone can help me, since I have been struggling with this problem for a few days now, scavenging the darkest corners of the web. 我真的希望有人能为我提供帮助,因为我已经为这个问题苦苦挣扎了几天,以消除网络上最黑暗的角落。

First of all I should tell you that my application is also using WCF - therefore the methods should be called through the WCFService to the Client. 首先,我应该告诉您我的应用程序也正在使用WCF-因此,应通过WCFService将方法调用给客户端。 Any help would be greatly appreciated! 任何帮助将不胜感激!

PS: If it helps here is a screenshot of my Solution overview: http://imgur.com/JV9Xl3K PS:如果有帮助,请查看我的解决方案概述的屏幕截图: http : //imgur.com/JV9Xl3K

Client Code: 客户代码:

  using Client.WCFServiceRef;  //My wcf Service reference

namespace Client
{
    public partial class MainWindow : Window
    {
        private WCFServiceClient Client;
        public MainWindow()
        {
            InitializeComponent();
            Client = new WCFServiceClient();
        }
        public string getMovieName()
        {
            string movieName = txtName.Text;
            return movieName;
        }
        public string getMovieLength()
        {
            string movieLength = txtLength.Text;
            return movieLength;
        }
        public string getMovieDesc()
        {
            string movieDesc = txtDescription.Text;
            return movieDesc;
        }
        private void btnMovies_Click(object sender, RoutedEventArgs e)
        {
            Client.addMovie(getMovieName(), getMovieLength(), getMovieDesc());
        }
    }
}

My MovieController code: 我的MovieController代码:

using Server.DB;

namespace Server.Control
{
    class MovieCtr
    {
        public void addMovie(string movieName, string movieLength, string movieDesc)
        {
            DBMovie conObj = new DBMovie();
            conObj.addMovie(movieName, movieLength, movieDesc);
        }
    }
}

My DBMovie Class 我的DBMovie类

namespace Server.DB
{
    public class DBMovie
    {
        DBConnection dbCon = new DBConnection();
        public void addMovie(string movieName, string movieLength, string movieDesc)  
        {
            dbCon.openConnection();
            SqlCommand com = new SqlCommand();
            string query = "INSERT into movies (Name, Runtime, Description) VALUES  ('" + movieName + "','" + movieLength + "','" + movieDesc + "');"; 
            com.CommandText = query;


        }
    }
}

Finally my dbconnection class 最后是我的dbconnection类

namespace Server.DB
{
    public class DBConnection
    {
        SqlConnection sc = new SqlConnection();
        public void openConnection()
        {
            try
            {
                sc.ConnectionString = ("Data Source=balder.ucn.dk;Initial Catalog=dmaa0213_6;User ID=dmaa0213_6;password=XXXXXX");
                sc.Open();
            }  //endTry
            catch (SqlException ex)
            {
                Console.WriteLine("Could not open connection to database");
                Console.WriteLine(ex);
            }
        }
        public void closeConnection()
        {
            sc.Close();
        }
    }
}

You need to call the ExecuteNonQuery on the SqlCommand object. 您需要在SqlCommand对象上调用ExecuteNonQuery。

string query = "INSERT into movies (Name, Runtime, Description) VALUES  ('" + movieName + "','"    + movieLength + "','" + movieDesc + "');"; 
com.CommandText = query;
com.ExecuteNonQuery();

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx

One more thing, you need to associate your connection to the command object. 还有一件事,您需要将连接与命令对象相关联。 The example in the web page above has the SqlConnection object being passed into the SqlCommand constructor. 上面的网页中的示例将SqlConnection对象传递到SqlCommand构造函数中。 You would need to rewrite the code for this to work properly. 您需要重写代码才能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM