C#窗体连接数据库的问题(新手求教!)
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# --------------------编程问答-------------------- form 改成 from
String Sql = String.Format("Select * from tempdb Londing where usemer ='{0}'and password='{1}'", name, password); --------------------编程问答-------------------- Select * form tempdb Londing where usemer ='{0}'and password='{1}'
(1)from
(2)and前加一个空格
(3)usemer这个拼写确认是否正确
(4)password加上括号:[password]
--------------------编程问答-------------------- form 单词拼错了 from 从。。 --------------------编程问答-------------------- tempdb Londing中间能空格? --------------------编程问答--------------------
这sql没有一处是对的。 --------------------编程问答-------------------- from好不好,真是的…… --------------------编程问答-------------------- 哥们,把你拼出来的sql语句,贴到sqlserver里运行一下;调正确以后,再把sql语句从sqlserver里拷到程序里来。 --------------------编程问答-------------------- 亲,查询语句就写错了,不是 form 而是 from --------------------编程问答-------------------- 先把sql语句运行正确了再跑吧。。。 --------------------编程问答-------------------- tempdb.dbo.Londing --------------------编程问答-------------------- String Sql = String.Format("Select count(*) from tempdb Londing where usemer ='{0}'and password='{1}'", name, password); --------------------编程问答-------------------- 先在数据库里面把SQL语句写对了,再到代码里面把条件替换一下吧,明显 from 关键字不对,以及 from 和 where 之间的两个单词不能有空格啊。 --------------------编程问答-------------------- 谢谢,大家的指教,原来的from附近有错误的问题解决了,现在我综合了一下大家对我的指正,出现了如图所示的问题,修改from与where之间的单词为大家所说的任何种形式都是错的。。。。。目前是提示对象名tempdbLonding无效,请问现在应该咋么处理呢? --------------------编程问答-------------------- 你数据库里有没有tempdbLonding这个表啊,是不是查Londing表?
如果是,你改成[tempdb].[dbo].[Londing] --------------------编程问答-------------------- 赞同! --------------------编程问答-------------------- 好吧,你的SQL语句还真没一处是对的。
SELCT * FROM 表名(表名有空格的话,用[]括起来.) WHERE 条件
每个单词都必须有空格间隔开!! --------------------编程问答--------------------
用7楼的方法吧 --------------------编程问答-------------------- 谢谢各位大神,我有点头绪了。。。。。已经弄出来了 --------------------编程问答-------------------- 哈哈,不是什么例子抄下来就可以运行成功的。。。操作数据库功能前提是有个数据库,知道登录IP,用户名,密码,表机构及成员。。。。。要对应理解。。。
补充:.NET技术 , C#