[英]Visual studio not loading data from Microsoft SQL Server Management Studio
I am try to retrieve the data from my table which I created on my SSMS.我尝试从我在 SSMS 上创建的表中检索数据。 I created a list box in a form of visual studio, and I try to display the data from database, but it doesn't send anything when I try to load up the program.
我以visual studio的形式创建了一个列表框,我尝试显示数据库中的数据,但是当我尝试加载程序时它没有发送任何内容。 The database is seems to look good.
数据库看起来不错。 That problem appear when I try to retrieve the movie_id and movie_title.
当我尝试检索movie_id 和movie_title 时出现该问题。 Here is my code:
这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DataBase
{
public partial class Form1 : Form
{
DataTable dt = new DataTable();
public void LoadData()
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;"+
"Initial Catalog=online_tv;Integrated Security=SSPI;");
SqlCommand cmd = new SqlCommand("SELECT movie.* FROM movie", conn);
SqlDataAdapter sa = new SqlDataAdapter(cmd);
conn.Open();
sa.Fill(dt);
conn.Close();
sa.Dispose();
cmd.Dispose();
conn.Dispose();
}
public class MyMovie
{
public int id;
public string title;
public override string ToString()
{
return title;
}
}
public void ShowMovies()
{
int i;
for (i = 0; i < dt.Rows.Count; i++)
{
MyMovie movie = new MyMovie();
movie.id = Convert.ToInt32(dt.Rows[i]["movie_id"]);
movie.title = Convert.ToString(dt.Rows[i]["movie_title"]);
listBox1.Items.Add(movie);
}
}
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
LoadData();
ShowMovies();
}
There are a bucketload of optimizations to be made here, but I'd like to post an answer that introduces a small but fundamental change that will make your life significantly easier.这里有一大堆优化要做,但我想发布一个答案,介绍一个小而根本的变化,这将使您的生活变得更加轻松。 Nearly every single line of code you've written there, you can get Visual Studio to write for you;
您在那里编写的几乎每一行代码,都可以让 Visual Studio 为您编写; just like you get it to write code when you lay out a Form, you can have it do all of this data access stuff too, and if you've managed to get SSMS connected, then getting VS connected is virtually the same process and will mean it just works:
就像你在布局表单时让它编写代码一样,你也可以让它做所有这些数据访问的事情,如果你已经设法连接 SSMS,那么连接 VS 几乎是相同的过程,并且意味着它只是有效:
SELECT * FROM movie
inSELECT * FROM movie
放入movie
node out of the Data Sources window and onto the formmovie
节点拖出“数据源”窗口并拖到窗体上You'll see your data你会看到你的数据
Now that you've scratched the surface of this, you can start playing with other stuff.现在您已经了解了这一点,您可以开始玩其他东西了。 Throw the
DataTable dt
away;扔掉
DataTable dt
; you won't need it.你不需要它。 Your form has a
dataset
object on it, that contains a Movie property that is a MovieDataTable - a subclass of datatable that you can access in a more logical and modern manner than via "string column names" - you say, for example, yourdatasetnamehere.Movies[0].movie_title
and it's already a string (time to swap your column names to PascalCase by the way) rather than somedatatable.Rows[0]["movie_title"].ToString()
您的表单上有一个
dataset
对象,其中包含一个 Movie 属性,它是一个 MovieDataTable - 一个数据表的子类,您可以以比通过“字符串列名称”更合乎逻辑和现代的方式访问 - 例如,您在这里说的是yourdatasetnamehere.Movies[0].movie_title
它已经是一个字符串(顺便把你的列名换成 PascalCase)而不是somedatatable.Rows[0]["movie_title"].ToString()
To show your movies in a listbox,要在列表框中显示您的电影,
movieBindingSource
movieBindingSource
movie_title
movie_title
(All this is done visually on the forms designer, not in code) (所有这些都是在表单设计器上直观地完成的,而不是在代码中)
You can consider throwing the Movie POCO away too;您也可以考虑将 Movie POCO 扔掉; a MovieDataRow is created by the dataset generator;
一个 MovieDataRow 由数据集生成器创建; it has all the properties of your movie, in a strongly typed fashion, just like your POCO does.
它以强类型方式拥有您电影的所有属性,就像您的 POCO 一样。 The tableadapter downloads the DB data and turns it into strongly typed MovieDataRow objects for you, meaning you can throw all the POCO mapping stuff in ShowMovies away too, and finally, databinding your listbox means you can toss out the bit where you build its items collection manually.
tableadapter 下载 DB 数据并将其转换为强类型的 MovieDataRow 对象,这意味着您也可以丢弃 ShowMovies 中的所有 POCO 映射内容,最后,数据绑定您的列表框意味着您可以丢弃构建其项目集合的位手动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.