用C#连接SQL后listbox显示不对齐
这是我的代码:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSy...
这是我的代码:
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 连接数据库
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("Data Source=J7MPVHBRQBDX4QI;initial catalog=library;integrated security=true");
SqlCommand cmd = new SqlCommand("select count(*) from Book",cn);
cn.Open();
int number = (int)cmd.ExecuteScalar();
cn.Close();
MessageBox.Show(number.ToString());
}
private void button2_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("Data Source=J7MPVHBRQBDX4QI;initial catalog=library;integrated security=true");
SqlCommand cmd = new SqlCommand("select * from Book",cn);
cn.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
listBox1.Items.Add("ID:"+dr.GetInt32(0)+" name:"+dr.GetString(1)+" author:"
+dr.GetString(2)+" price:"+dr.GetSqlMoney(3));
}
}
}
}
}
想让那几个数据对齐,刚学不懂,是不是要在数据库上改,我用的是SQL SERVER 2008和VS2010 展开
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 连接数据库
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("Data Source=J7MPVHBRQBDX4QI;initial catalog=library;integrated security=true");
SqlCommand cmd = new SqlCommand("select count(*) from Book",cn);
cn.Open();
int number = (int)cmd.ExecuteScalar();
cn.Close();
MessageBox.Show(number.ToString());
}
private void button2_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("Data Source=J7MPVHBRQBDX4QI;initial catalog=library;integrated security=true");
SqlCommand cmd = new SqlCommand("select * from Book",cn);
cn.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
listBox1.Items.Add("ID:"+dr.GetInt32(0)+" name:"+dr.GetString(1)+" author:"
+dr.GetString(2)+" price:"+dr.GetSqlMoney(3));
}
}
}
}
}
想让那几个数据对齐,刚学不懂,是不是要在数据库上改,我用的是SQL SERVER 2008和VS2010 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询