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;  //连接数据库的头文件namespace WinFormSQL_exe
{
    /// <summary>
    ///连接数据库流程:
    ///1.创建连接字符串: String conn = "Data Source=.;Initial Catalog=winformSQL;Integrated Security=True";
    ///2.创建连接对象  : SqlConnection connect = new SqlConnection(conn); 
    ///3.创建sql语句 :string sql="";
    ///4.创建SqlCommand对象: SqlCommand command = new SqlCommand(Sql, connect);
    ///5.打开数据库连接 connet.open()
    ///6.执行命令:int num = Convert.ToInt32(command.ExecuteScalar());
    ///7.关闭数据库:
    /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            String name = this.textBox1.Text;
            String password = this.textBox2.Text;
            //获得数据库连接字符串Data Source=.;Initial Catalog=tempdb;Integrated Security=True
            String conn = "Data Source=.;Initial Catalog=tempdb;Integrated Security=True";
            //创建SqlConnection        
            SqlConnection connect = new SqlConnection(conn);
            String Sql = String.Format("Select * form tempdb Londing where usemer ='{0}'and password='{1}'", name, password);
            //创建SqlCommand对象
            SqlCommand command = new SqlCommand(Sql, connect);
            connect.Open();
            int num = Convert.ToInt32(command.ExecuteScalar());
            try
            {
                if (num > 0)
                {
                    MessageBox.Show("登陆成功!");
                }
                else
                {
                    MessageBox.Show("账户或密码错误!");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("异常!"+ex);            }
            finally
            {
                connect.Close();
            }
            
        }
    }
}
这些是我从视频上学习的,但是在点击登录的时候是错的,总是提示说form或者是*号附近有错误,具体我不知道是什么原因,望大神们指点一下。。c#

解决方案 »

  1.   

    form 改成 from  String Sql = String.Format("Select * from tempdb Londing where usemer ='{0}'and password='{1}'", name, password);
      

  2.   

    Select * form tempdb Londing where usemer ='{0}'and password='{1}'
    (1)from
    (2)and前加一个空格
    (3)usemer这个拼写确认是否正确
    (4)password加上括号:[password]
      

  3.   

    form  单词拼错了  from  从。。
      

  4.   

    哥们,把你拼出来的sql语句,贴到sqlserver里运行一下;调正确以后,再把sql语句从sqlserver里拷到程序里来。
      

  5.   

    亲,查询语句就写错了,不是 form 而是 from 
      

  6.   

    先把sql语句运行正确了再跑吧
      

  7.   

    String Sql = String.Format("Select count(*) from tempdb Londing where usemer ='{0}'and password='{1}'", name, password);
      

  8.   

    先在数据库里面把SQL语句写对了,再到代码里面把条件替换一下吧,明显  from 关键字不对,以及 from 和 where 之间的两个单词不能有空格啊。
      

  9.   

    谢谢,大家的指教,原来的from附近有错误的问题解决了,现在我综合了一下大家对我的指正,出现了如图所示的问题,修改from与where之间的单词为大家所说的任何种形式都是错的。。目前是提示对象名tempdbLonding无效,请问现在应该咋么处理呢?
      

  10.   

    你数据库里有没有tempdbLonding这个表啊,是不是查Londing表?
    如果是,你改成[tempdb].[dbo].[Londing]
      

  11.   

    好吧,你的SQL语句还真没一处是对的。SELCT * FROM 表名(表名有空格的话,用[]括起来.) WHERE 条件每个单词都必须有空格间隔开!!