C#使用SqlConnection,SqlCommand,SqlDataReader和SqlParameter,写一个方法从数据库中读数据。
2013-08-20
展开全部
public SqlDataReader getData(string pid){ SqlConnection conn=new SqlConnection("server=.;uid=sa;pwd=;database=Northwind"); conn.Open(); SqlCommand cmd=new SqlCommand("select * from Products where ProductID=@pid",conn); cmd.Parameter=new SqlParameter("@pid",pid); SqlDataReader DR=cmd.ExecuteReader(); return DR;} 这代码表示通过一个产品ID参数查询数据库中产品表,返回一个SqlDataReader数据集。今天刚把编程环境卸了,所以只能手敲代码了。如有小错误,请自行修改!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-20
展开全部
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 proc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
try
{
DBHelper.connection.Open();
string sql = "proc_del";
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@name", SqlDbType.Char));
command.Parameters["@name"].Value = "张三";
command.Parameters["@name"].Direction = ParameterDirection.Input;
command.Parameters.Add(new SqlParameter("@count", SqlDbType.Int));
command.Parameters["@count"].Direction = ParameterDirection.Output;
command.ExecuteNonQuery();
txtCount.Text = command.Parameters["@count"].Value.ToString();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
listView1.Items.Add(new ListViewItem(reader[0].ToString()));
}
reader.Close(); }
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
DBHelper.connection.Close();
}
}
}
}
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 proc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
try
{
DBHelper.connection.Open();
string sql = "proc_del";
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@name", SqlDbType.Char));
command.Parameters["@name"].Value = "张三";
command.Parameters["@name"].Direction = ParameterDirection.Input;
command.Parameters.Add(new SqlParameter("@count", SqlDbType.Int));
command.Parameters["@count"].Direction = ParameterDirection.Output;
command.ExecuteNonQuery();
txtCount.Text = command.Parameters["@count"].Value.ToString();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
listView1.Items.Add(new ListViewItem(reader[0].ToString()));
}
reader.Close(); }
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
DBHelper.connection.Close();
}
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-20
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;using System.Data.SqlClient;public class sqlServerConnection
{
public string AttachDBFilename { get; set; } public static void Main()
{
try
{
string connectString = @"Server=(local);
Integrated Security=true"; SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectString); Console.WriteLine("Original: " + builder.ConnectionString);
Console.WriteLine();
Console.WriteLine("AttachDBFileName = {0}", builder.AttachDBFilename); builder.AttachDBFilename = @"E:\vc#数据库\图书管理系统\library.mdf";
Console.WriteLine("Modified: " + builder.ConnectionString); using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
{
connection.Open();
Console.WriteLine();
Console.WriteLine("Database = " + connection.Database);
}
Console.WriteLine("Press any key to finish.");
Console.ReadLine();
}
catch (System.Data.SqlClient.SqlException ex)
{
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;using System.Data.SqlClient;public class sqlServerConnection
{
public string AttachDBFilename { get; set; } public static void Main()
{
try
{
string connectString = @"Server=(local);
Integrated Security=true"; SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectString); Console.WriteLine("Original: " + builder.ConnectionString);
Console.WriteLine();
Console.WriteLine("AttachDBFileName = {0}", builder.AttachDBFilename); builder.AttachDBFilename = @"E:\vc#数据库\图书管理系统\library.mdf";
Console.WriteLine("Modified: " + builder.ConnectionString); using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
{
connection.Open();
Console.WriteLine();
Console.WriteLine("Database = " + connection.Database);
}
Console.WriteLine("Press any key to finish.");
Console.ReadLine();
}
catch (System.Data.SqlClient.SqlException ex)
{
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询