帮你的连接数据库的sql语句写出来看看?
解决方案 »
- 数据库表A的数据。插入到表B的实现方法
- 急急急急急急急急急急急急急急急急!!!GridView中添加一个百分比的样式
- 关于分页问题请教
- 急急急!!在使用webservices和调用js时,出现了运行时间上的错误,应如何解决
- vs2005出现这样的错误?
- 怎么在TabContainer里面不能用UpdatePanel1?
- 怎样把号码显示的后四位用“*“号代替!!!!
- 大家开发ASP.NET都用什么语言,介绍一下吧,C#? VB.NET? VC.NET?那个比较大众化?
- 请问得到用rows得到记录的总数后,怎么取得最后一条记录的ID字段的值!请看我的代码,谢谢!
- 本机用验证控件作了验证,测试无问题,传到网上后却不起作用了?
- 那里有visual stuido.net中文版下载??
- 怎么把Datagrid里的数据写入数据库
在代码中只写了 me.sqldataadapter1.fill(cwqdata)
me.databind()然后运行时,它总提示:用cwq\aspnet登录失败。
public void getconn()
{
try
{
SqlDataAdapter myad = new SqlDataAdapter();
SqlConnection mycon = new SqlConnection("data source = JSJ227\\SWFCCID ;"
+"initial catalog=dgl;"
+"persist security info=False;"
+"user id=sa;workstation id=JSJ227;"
+"packet size=4096");
DataSet myset = new DataSet();
string textcom="SELECT * FROM 学生成绩表$"; myad.SelectCommand = new SqlCommand(textcom,mycon); myad.Fill( myset,"学生成绩表$"); dataGrid1.DataSource=myset.Tables["学生成绩表$"].DefaultView ;
dataGrid1.Refresh();}
catch( Exception rc)
{
MessageBox.Show("错误" + rc.Message );
}
using System.Data;
using System.Data.SqlClient;
using System.Security.Cryptography; //使用RSA加密解密类
using System.Windows.Forms;
using System.IO;
using System.Text;namespace ReadPubs
{
/// <summary>
/// DataOperate 的摘要说明。
/// </summary>
public class DataOperate
{
public static string StrConn; //申明通用数据库连接字符串 公用变量
private System.Data.SqlClient.SqlConnection sqlConnection;
private System.Data.SqlClient.SqlCommand sqlCommand;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter;
private System.Data.SqlClient.SqlDataReader sqlDataReader=null;
private System.Data.DataSet dataSet;
private System.Data.DataTable dataTable=null; private RSACryptoServiceProvider MyRSA=new RSACryptoServiceProvider();
public string SetStrConn
{
get
{
return StrConn;
}
set
{
StrConn=value;
}
} public DataOperate()
{
//
// TODO: 在此处添加构造函数逻辑
//
sqlConnection=new SqlConnection(StrConn);
} public DataOperate(string ConnStr)
{
StrConn=ConnStr;
sqlConnection=new SqlConnection(StrConn);
}
public DataOperate(string ConnDBPath,bool ToStrConn)
{
if(ToStrConn)
StrConn=GetConnStr(ConnDBPath);
sqlConnection=new SqlConnection(GetConnStr(ConnDBPath));
} public void ExecuteSqlCommand(string StrSql) //StrSql为Insert、Delete、Update等SQL语句
{ //执行传入的SQL语句
try
{
sqlConnection.Open();
sqlCommand=new SqlCommand(StrSql,sqlConnection);
sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
sqlConnection.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
sqlConnection.Close();
}
} public DataTable ExecuteTable(string StrSql)//执行传入的Select SQL语句返回数据表
{
try
{
sqlConnection.Open();
sqlDataAdapter=new SqlDataAdapter(StrSql,sqlConnection);
dataSet=new DataSet();
sqlDataAdapter.Fill(dataSet,"StrTable");
dataTable=dataSet.Tables["StrTable"];
//dataSet.Clear();
sqlDataAdapter.Dispose();
sqlConnection.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
sqlConnection.Close();
}
return dataTable;
}
public SqlDataReader SqlReader(string StrSql)
{
SqlDataReader sqlReader=null;
try
{
sqlConnection.Open();
sqlCommand=new SqlCommand(StrSql,sqlConnection);
sqlReader=sqlCommand.ExecuteReader();
sqlCommand.Dispose();
sqlConnection.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
sqlConnection.Close();
}
return sqlReader;//调用后要注间关闭读者对象
}
public DataSet ExecuteDataSet(string StrSql,string StrTableName)//执行传入的Select SQL语句返回DataSet
{ //StrTableName为DataSet中的表名
dataSet=new DataSet();
try
{
sqlConnection.Open();
sqlDataAdapter=new SqlDataAdapter(StrSql,sqlConnection);
//dataSet.Tables.Add(StrTableName);
//sqlDataAdapter.Fill(dataSet,StrTableName);
sqlDataAdapter.Fill(dataSet);
sqlDataAdapter.Dispose();
sqlConnection.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
sqlConnection.Close();
}
return dataSet;
}
public int GetMaxNum(string StrSql)
{
int MaxNumber=0;
try
{
sqlConnection.Open();
sqlCommand=new SqlCommand(StrSql,sqlConnection);
sqlDataReader=sqlCommand.ExecuteReader();
if(sqlDataReader.Read())
{
if(sqlDataReader[0].ToString()!="")
MaxNumber=(int)sqlDataReader[0];
}
/*else
{
MaxNumber="0001";
}*/
sqlCommand.Dispose();
sqlDataReader.Close();
sqlConnection.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
sqlConnection.Close();
}
return MaxNumber;
}
public int GetRecordCount(string StrSql)//执行传入的Select SQL语句得到记录总数
{
int recordCount=0;
try
{
recordCount=ExecuteTable(StrSql).Rows.Count;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
return recordCount;
}
public string GetConnStr(string StrDBPath)
{
string MyStrConn=null;
string TempReadLine=null;
try
{
Stream SReadLine=File.OpenRead(StrDBPath);
StreamReader SrReadLine=new StreamReader(SReadLine,System.Text.Encoding.ASCII);
SrReadLine.BaseStream.Seek(0,SeekOrigin.Begin); while(SrReadLine.Peek()>-1)
{
TempReadLine=SrReadLine.ReadLine();
if(TempReadLine.EndsWith(";"))
MyStrConn+=TempReadLine;
else
MyStrConn+=TempReadLine+";";
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
return MyStrConn;
} public string EncryptString(string StrSend)
{
byte[] StrTxt=Encoding.Unicode.GetBytes(StrSend.ToCharArray());
byte[] MyByte=MyRSA.Encrypt(StrTxt,false);
return Encoding.Unicode.GetString(MyByte);
} public string DecryptString(string StrSend)
{
byte[] StrTxt=Encoding.Unicode.GetBytes(StrSend.ToCharArray());
byte[] MyByte=MyRSA.Decrypt(StrTxt,false);
return Encoding.Unicode.GetString(MyByte);
}
}
}
那个组件就不给你了,那代码太长了,
就OK了!
在SQL SERVER企业管理器的安全性结点的登录里添加一个cwq\aspnet用户并设置权限就OK了,