C#连接数据库

 

连接数据库:

try
{

SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source = (local); Initial Catalog =manage; Integrated Security = SSPI;";
conn.Open();
if (conn.State == ConnectionState.Open)
{


string sqlstr =
"select * from 管理员登陆表 where id=@id";

SqlCommand cmd = new SqlCommand(sqlstr, conn);
cmd.Parameters.AddWithValue("id", textBox1.Text);

SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string ss=dr["pass"].ToString();
int i;
for ( i = 0; i < textBox2.Text.Length; i++)
if (ss[i] == textBox2.Text[i])
{
continue;
}
else break;
if (i==textBox2.Text.Length )
s = "s";
}
}

conn.Close();

}
catch (Exception ee)
{
MessageBox.Show(" 查询出错! " + ee.Message);
}
finally {
if (s == "s")
{
user ur = new user(Convert.ToInt32(textBox1.Text), textBox2.Text);
Program.u = ur;
Program.sigin = true;
this.Close();
}
else
{
MessageBox.Show(" 无此账号或密码错误! ");

}
}

把查询的信息添加到dataview中:

try{
string s="";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source = (local); Initial Catalog =manage; Integrated Security = SSPI;";
conn.Open();
if (conn.State == ConnectionState.Open)
{

string sqlstr = "Select * from 我的订单 ";//where 计划交货>"+DateTime.Now;
SqlCommand cmd = new SqlCommand(sqlstr, conn);

try
{
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
s="";
s=dr["制作单号"].ToString()+",";
s+=dr["盒号"].ToString()+",";
s+=dr["是否返工"].ToString()+",";
s+=dr["公司名称"].ToString()+",";
s+=dr["地区"].ToString()+",";
s+=dr["主管姓名"].ToString()+",";
s+=dr["客户单号"].ToString()+",";
s+=dr["计划交货"].ToString();
addTodataview(s);

}
}
catch (Exception mye)
{
MessageBox.Show(" 提醒有误!\n" + mye.Message);

}


}

conn.Close();

}
catch (Exception ex)
{
MessageBox.Show("保存连接失败" + ex.Message);

}

 

private void addTodataview(string s)
{

try
{
string[] sp = s.Split(',');
dataGridView1.ClearSelection();
dataGridView1.Rows.Add(sp);

}
catch (Exception ex)
{
MessageBox.Show("查询失败!\n" + ex.Message);

}

}

 

转载于:https://www.cnblogs.com/FuLiuWei/p/SQLData.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据库的连接,基本的连接方法namespace data { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.button1 = new System.Windows.Forms.Button(); this.dataGridView1 = new System.Windows.Forms.DataGridView(); this.aaDataSet = new data.aaDataSet(); this.aaDataSetBindingSource = new System.Windows.Forms.BindingSource(this.components); this.Column1 = new System.Windows.Forms.DataGridViewButtonColumn(); this.Column2 = new System.Windows.Forms.DataGridViewButtonColumn(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.aaDataSet)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.aaDataSetBindingSource)).BeginInit(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(110, 227); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // dataGridView1 // this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.Column1, this.Column2}); this.dataGridView1.Location = new System.Drawing.Point(32, 12); this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.RowTemplate.Height = 23; this.dataGridView1.Size = new System.Drawing.Size(240, 150); this.dataGridView1.TabIndex = 1; // // aaDataSet // this.aaDataSet.DataSetName = "aaDataSet"; this.aaDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema; // // aaDataSetBindingSource // this.aaDataSetBindingSource.DataSource = this.aaDataSet; this.aaDataSetBindingSource.Position = 0; // // Column1 // this.Column1.HeaderText = "Column1"; this.Column1.Name = "Column1"; // // Column2 // this.Column2.HeaderText = "Column2"; this.Column2.Name = "Column2"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(284, 262); this.Controls.Add(this.dataGridView1); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.aaDataSet)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.aaDataSetBindingSource)).EndInit(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.Button button1; private System.Windows.Forms.DataGridView dataGridView1; private System.Windows.Forms.DataGridViewButtonColumn Column1; private System.Windows.Forms.DataGridViewButtonColumn Column2; private aaDataSet aaDataSet; private System.Windows.Forms.BindingSource aaDataSetBindingSource; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值