.net c#保存到DATABASE MVC模式

Saving to DATABASE MVC pattern
2021-02-23
  •  译文(汉语)
  •  原文(英语)

我是编程新手,请耐心等待.我在整个项目的不同类中编写了以下代码,我想知道为什么我的输入没有存储在数据库中.我真的希望有人能为我提供帮助,因为我已经为这个问题苦苦挣扎了几天,以消除网络上最黑暗的角落.

首先,我应该告诉您我的应用程序也正在使用WCF-因此,应通过WCFService向客户端调用方法.任何帮助将不胜感激!

PS:如果有帮助,请参见我的解决方案概述的屏幕截图:http : //imgur.com/JV9Xl3K

客户代码:

  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());
        }
    }
}

我的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);
        }
    }
}

我的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;


        }
    }
}

最后是我的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();
        }
    }
}
解决过程1

您需要在SqlCommand对象上调用ExecuteNonQuery.

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

http://msdn.microsoft.com/zh-CN/library/system.data.sqlclient.sqlcommand.executenonquery.aspx

还有一件事,您需要将连接与命令对象相关联.上面的网页中的示例将SqlConnection对象传递到SqlCommand构造函数中.您需要重写代码才能正常工作.

速聊1:
这似乎确实有作用,但是现在导致单击该按钮时应用程序崩溃.(猜测要比以前的按钮似乎没做多好.
速聊2:
如前所述,问题是我真的是编程新手,不太确定你的意思.
速聊3:
例如,请查看本文.确保正确使用using语句很重要.这将确保在完成SQL连接后正确处理它.(hanselman.com/blog/…)
速聊4:
问题是我不完全了解在这种情况下USING会做什么.我只知道在课程开始时如何使用它
速聊5:
如果对象实现IDisposable接口,则需要调用Dispose方法以释放非托管资源.USING关键字确保在超出范围时调用Dispose方法.这是尝试/捕捉/最终使用的捷径.(stackoverflow.com/questions/5563886/…)SqlConnection和SqlCommand都实现IDisposable接口.

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. Any help would be greatly appreciated!

PS: If it helps here is a screenshot of my Solution overview: 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:

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

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

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();
        }
    }
}
Solutions1

You need to call the ExecuteNonQuery on the SqlCommand object.

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

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. You would need to rewrite the code for this to work properly.

Talk1:
That did seem to do something, but now results in the application crashing when the button is clicked. (guess that's better than before where the button didn't seems to do much.
Talk2:
The problem is, as stated, I am really new to programming, and not quite sure what you mean by that.
Talk3:
For an example, check out this article. It is important to make sure that the using statement is being used properly. It will ensure that the SQL connection is properly disposed when it is completed. (hanselman.com/blog/…)
Talk4:
The problem is that I don't understand exactly what the USING does in that context. I only know how to use it in the beginning of a class
Talk5:
If a object implements the IDisposable interface, then the Dispose method needs to be called to release unmanaged resources. The USING keyword ensure that Dispose method is called when it goes out of scope. It is a shortcut to having a use a try/catch/finally. (stackoverflow.com/questions/5563886/…) Both SqlConnection and SqlCommand implements the IDisposable interface.
转载于:https://stackoverflow.com/questions/23712830/saving-to-database-mvc-pattern

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入