Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在哪里/如何在C代码中编写T-SQL并获得结果(Visual Studio 2008)_C#_Visual Studio_Tsql - Fatal编程技术网

C# 在哪里/如何在C代码中编写T-SQL并获得结果(Visual Studio 2008)

C# 在哪里/如何在C代码中编写T-SQL并获得结果(Visual Studio 2008),c#,visual-studio,tsql,C#,Visual Studio,Tsql,我正在尝试在C visual studio中编写t-sql。我有以下代码连接到数据库: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windo

我正在尝试在C visual studio中编写t-sql。我有以下代码连接到数据库:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using Microsoft.SqlServer.Server;

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string connetionString = null;
            SqlConnection cnn;
            connetionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Xtreme\\Desktop\\CardsDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            cnn = new SqlConnection(connetionString);
            try
            {


                cnn.Open();
                MessageBox.Show("Connection Open ! ");

                cnn.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! ");
            }


        }
    }
}
在哪里/如何编写T-SQL代码以及如何获得结果

有人能在我的代码中给我一个简单的select示例吗?

创建一个SqlCommand并将CommandType设置为CommandType.Text。然后将SQL添加到SqlCommand的CommandText属性中

您可以使用以下方法:


Ardman已经向您展示了如何执行任意sql命令。但我没有很好地回答,就是把sql语句放在哪里

我认为这是一种非常糟糕的行为,如果你把你的语句直接放进代码中去读也不是很好

在我看来,更好的方法是:

在项目中创建一个新文件夹,可能称为“查询” 右键单击此文件夹并选择“添加-新建项目” 在对话框中,只需选择Textfile并为其提供有关此查询将执行的操作的名称 确保将文件扩展名从.txt替换为.sql 把你的陈述放在这个文件里 在资源编辑器中,将此文件添加为资源 现在,您只需使用Properties.Resources.MySqlStatement就可以在项目中访问此sql语句
SqlCommand command = new SqlCommand(commandName, (SqlConnection)Connection);
command.CommandType = CommandType.Text;
command.CommandText = "SELECT * FROM MyTable";
IDataReader result = command.ExecuteReader();
try
{
    using (SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM Employee", cnn))
    {
        // Use DataAdapter to fill DataTable
        DataTable t = new DataTable();
        a.Fill(t);

        // Render data onto the screen
        dataGridView1.DataSource = t; //if you want.
    }
}
catch (Exception ex)
{
    MessageBox.Show("Problem!");
}