using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string sqlData = "Data Source =.;Initial Catalog =Student11;uid=sa;pwd=3661849";
            SqlConnection cmd = new SqlConnection(sqlData);
            SqlCommand com = cmd.CreateCommand();        }
    }
}

解决方案 »

  1.   

    很少见有人在ConsoleApp里面要连接数据库的。而且你这个问题还分析不出来你想做什么,哪儿要补全!
      

  2.   

    只要在cmd上能把数据数出来就行,实在不会,刚学C#,这是看视屏的某一段,但是太模糊了,后面的看不清楚他写了什么。
      

  3.   

    你的数据库里有什么我也不知道啊,有什么表?有什么字段?我假设一段吧。                System.Data.SqlClient.SqlConnection conn = new SqlConnection(connStr); //创建连接对象
                    System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand(); //创建命令对象
                    cmd.CommandText = "select Name from [User] where ID=1"; //定义查询语句
                    conn.Open(); //打开数据库
                    Console.WriteLine(cmd.ExecuteScalar());
                    conn.Close(); //关闭连接
                    conn.Dispose(); //释放资源
      

  4.   

    ExecuteScalar要包含在什么Using中,这里报错。
      

  5.   

    System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr); //创建连接对象
      

  6.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                string sqlData = "Data Source =.;Initial Catalog =Student11;uid=sa;pwd=3661849";
                SqlConnection cmd = new SqlConnection(sqlData);
                SqlCommand com = cmd.CreateCommand();
                com.CommandText = "SELECT * FROM Student_1";//包括ID(int),name(vachar),picture(image)。
                cmd.Open();
                com.Connection = cmd; 
                Console.WriteLine (cmd.ExecuteScalar());
                cmd.Close();
            }
        }
    }//报错
      

  7.   

    没有任何基础,老师就叫我做这东西,查了两天资料,只会创建个SQL数据库,还要实现用C#的winform实现插入数据到SQl数据库
      

  8.   


    我太无语了,你太新了!没看别人都不理你么5555555555555555你的问题得系统的教才行,我来不了了,我给你个建议,你去翻MSDN吧,没安装的话,上在线的http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlcommand_methods.aspx
      

  9.   

    这是我一年前的数据库连接带吧,应该会对你有点用string connectionString;
            public SqlDB()
            {
                connectionString = System.Configuration.ConfigurationSettings.AppSettings["connectionString"];
            }
            public bool ExecutInsert(string StrCommand)
            {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = connectionString;
                conn.Open();            SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = StrCommand;
                if (comm.ExecuteNonQuery() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
    ....<appSettings>
        <add key="connectionString" value="Data source=WLF-006\SQLEXPRESS;Database=RockTest;Trusted_Connection=True;"  />
    </appSettings>这是在web。config
      

  10.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;namespace 显示表中数据2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                SqlConnection mysql = new SqlConnection();
               mysql.ConnectionString = "Data Source=localhost;Initial  Catalog=MyBookShop;Integrated Security=True";//数据库名
                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection = mysql;
               sqlCommand.CommandType = CommandType.Text;
               sqlCommand.CommandText = "SELECT * FROM Category";//表名
                mysql.Open();
                SqlDataReader sqlDateRreader;
                sqlDateRreader = sqlCommand.ExecuteReader();
                while (sqlDateRreader.Read())
                {
                    richTextBox1.Text += "  " + sqlDateRreader["CategoryID"].ToString() + "\t"
                    + sqlDateRreader["CategoryName"].ToString() + "\r";//表里面有两列---CategoryID和CategoryName            }
                Console.WriteLine(richTextBox1.Text);
                mysql.Close();
                sqlDateRreader.Close();
            }
        }
    }