简体   繁体   English

Visual Studio 未从 Microsoft SQL Server Management Studio 加载数据

[英]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 几乎是相同的过程,并且意味着它只是有效:

  • Add a DataSet type of file to your project, and open it将 DataSet 类型的文件添加到您的项目中,并打开它
  • Right click on it and choose add a TableAdapter - this is like a DataAdapter on steroids右键单击它并选择添加一个 TableAdapter - 这就像类固醇上的 DataAdapter
  • Add a new conenction - the dialog is virtually the same as SSMS so you should be able to connect your DB without any hassle添加新连接 - 该对话框几乎与 SSMS 相同,因此您应该能够轻松连接数据库
  • Say you want to write "a query that returns rows" - put SELECT * FROM movie in假设您要编写“返回行的查询” - 将SELECT * FROM movie放入
  • Finish the wizard完成向导
  • Right click the "Fill" line in the tableadapter and choose Preview Data, hit the button and see your data.右键单击 tableadapter 中的“填充”行并选择预览数据,点击按钮并查看您的数据。 If you see no data, you connected to a database that is devoid of data如果看不到数据,则连接到没有数据的数据库
  • Go to your form, open the Data Sources window (View menu.. Other Windows)转到您的表单,打开数据源窗口(查看菜单.. 其他窗口)
  • Drag the movie node out of the Data Sources window and onto the formmovie节点拖出“数据源”窗口并拖到窗体上
  • Run the app运行应用程序

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,要在列表框中显示您的电影,

  • add a listbox to the form向表单添加列表框
  • set the DataSource property to be the movieBindingSource将 DataSource 属性设置为movieBindingSource
  • Set the DisplayMember to be movie_title将 DisplayMember 设置为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.

相关问题 Sql 服务器未在 Visual Studio 中加载 - Sql server is not loading in visual studio 初始安装和从Visual Studio连接到Microsoft SQL Server的问题 - trouble with initial setup and connection to microsoft sql server from Visual Studio Microsoft SQL Server与SQL Server Management Studio - Microsoft SQL Server vs. SQL Server Management Studio 如何在SQL Server Management Studio中使用Visual Studio创建连接数据库? - How connect database created in SQL Server Management Studio with Visual Studio? 用数据填充程序。.Visual Studio 2012和SQL Server Management Studio - Populating a program with data.. Visual Studio 2012 & SQL Server Management Studio 生成Web.config以将数据从文件加载到Visual Studio 2012中的MS SQL 2008服务器数据库 - generate Web.config for loading data from a file to MS SQL 2008 server database in Visual Studio 2012 按顺序排列数据 - SQL Server Management Studio - Ordering data in order - SQL Server Management Studio SQL Server Management Studio上的数据不是更新 - Data on SQL Server Management Studio is not Updating 将LocalDB附加到Microsoft SQL Server Management Studio会破坏数据库 - Attach LocalDB to microsoft sql server management studio corrupts database Microsoft SQL Server Management Studio说RowVersion是无效类型 - Microsoft SQL Server Management Studio says that RowVersion is invalid type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM