---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------
一个简单的控制台应用程序,作为ADO操作基础入门学习资料。
实现功能:用户输入用户名和密码,输入正确,则提示登录成功,
失败,则提示用户名或密码输入错误。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace DataTest
{
class Program
{
static void Main(string[] args)
{
string dataDir = AppDomain.CurrentDomain.BaseDirectory;
if (dataDir.EndsWith(@"\bin\Debug\")
|| dataDir.EndsWith(@"\bin\Release\"))
{
dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
AppDomain.CurrentDomain.SetData("DataDirectory",dataDir);
}
Console.WriteLine("请输入用户名:");
string username = Console.ReadLine();
Console.WriteLine("请输入密码:");
string password = Console.ReadLine();
using (SqlConnection conn = new SqlConnection( @"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\DataTest.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
using(SqlCommand cmd=conn.CreateCommand())
{
cmd.CommandText = "select count(*) from T_Users where UserName=@UN and Password=@P";
cmd.Parameters.Add(new SqlParameter("UN",username));
cmd.Parameters.Add(new SqlParameter("P",password));
int i = Convert.ToInt32(cmd.ExecuteScalar());
if (i > 0)
{
Console.WriteLine("登陆成功");
}
else
{
Console.WriteLine("用户名或者密码错误!");
}
}
}
Console.WriteLine("ok!");
Console.ReadKey();
}
}
}
---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------