How to write these code in DAL ,BAL and UI layer seperately - c#-4.0

Sqlconnectioncon=new sqlconnnection(conn);
Sqlcommand cmd=new sqlcommand(select name from project where name like '"+gridview.rows[e.rows].cells ["name"]+"',con);
SqlDataAdapter sda=new sqlDataAdapter(cmd);
Dataset ds=new Dataset()
sda.Fill(ds,"table");
Datarow row=ds.Tables["table"].rows[0];
nametextbox.text=row["name"]
.Tostring();
addresstextbox.text=row
["address"].Tostring();

Related

Transform JavaPairDStream to Tuple3 in Java

I am experimenting with the Spark job that streams data from Kafka and produces to Cassandra.
The sample I am working with takes a bunch of words in a given time interval and publishes the word count to Cassandra. I am also trying to also publish the timestamp along with the word and its count.
What I have so far is as follows:
JavaPairReceiverInputDStream<String, String> messages =
KafkaUtils.createStream(jssc, zkQuorum, groupId, topicMap);
JavaDStream<String> lines = messages.map(Tuple2::_2);
JavaDStream<String> words = lines.flatMap(x -> Arrays.asList(SPACE.split(x)).iterator());
JavaPairDStream<String, Integer> wordCounts = words.mapToPair(s -> new Tuple2<>(s, 1))
.reduceByKey((i1, i2) -> i1 + i2);
Now I am trying to append to these records the timestamp. What I have tried is something like this:
Tuple3<String, Date, Integer> finalRecord =
wordCounts.map(s -> new Tuple3<>(s._1(), new Date().getTime(), s._2()));
Which of course is shown as wrong in my IDE. I am completely new to working with Spark libraries and writing in this form (I guess lambda based) functions.
Can someone help me correct this error and achieve what I am trying to do?
After some searching done on the web and studying some examples I was able to achieve what I wanted as follows.
In order to append the timestamp attribute to the existing Tuple with two values, I had to create a simple bean with which represents my Cassandra row.
public static class WordCountRow implements Serializable {
String word = "";
long timestamp;
Integer count = 0;
Then, I had map the (word, count) Tuple2 objects in the JavaPairDStream structure to a JavaDStream structure that holds objects of the above WordCountRow class.
JavaDStream<WordCountRow> wordCountRows = wordCounts.map((Function<Tuple2<String, Integer>, WordCountRow>)
tuple -> new WordCountRow(tuple._1, new Date().getTime(), tuple._2));
Finally, I could call foreachRDD method on this structure (which returns objects of WordCountRow) which I can write to Cassandra one after the other.
wordCountRows.foreachRDD((VoidFunction2<JavaRDD<WordCountRow>,Time>)(rdd,time)->{
final SparkConf sc=rdd.context().getConf();
final CassandraConnector cc=CassandraConnector.apply(sc);
rdd.foreach((VoidFunction<WordCountRow>)wordCount->{
try(Session session=cc.openSession()){
String query=String.format(Joiner.on(" ").join(
"INSERT INTO test_keyspace.word_count",
"(word, ts, count)",
"VALUES ('%s', %s, %s);"),
wordCount.word,wordCount.timestamp,wordCount.count);
session.execute(query);
}
});
});
Thanks

Passing multiple rows to SQL Server Stored Procedure

I have a problem that I want to pass multiple rows of Excel file to a SQL Server stored procedure and I don't have any idea about it.
I have an Excel file to be imported, data needs to be inserted into three different tables. I have successfully imported data into one table as follows
string path = string.Concat((Server.MapPath("~/temp/"+FileUpload1.FileName)));
FileUpload1.PostedFile.SaveAs(path);
OleDbConnection oleCon = new OleDbConnection("Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + path + ";Extended Properties = Excel 12.0;");
OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]",oleCon);
OleDbDataAdapter dtap = new OleDbDataAdapter(cmd);
oleCon.Open();
DbDataReader rdr = cmd.ExecuteReader();
string con_str = #"Data source = .;Initial Catalog=practiceDB;Integrated security=True";
SqlBulkCopy bulkInsert = new SqlBulkCopy(con_str);
bulkInsert.DestinationTableName = "Excel_New";
bulkInsert.WriteToServer(rdr);
oleCon.Close();
Array.ForEach(Directory.GetFiles((Server.MapPath("~/temp/"))),File.Delete);
How to send rows to stored procedure rather than to any DestinationTable?
Rather than passing the rows as parameters, how about just reading from your table Excel_New the in the stored procedure?

Example about how to use map cql type with DataStax java driver

I am trying to use the datastax java driver to update and query a column family that has a map field. Does anyone an example about how to use cql collections with the Datastax Java Driver?
Thanks
I will add some examples of using the CQL collections with both simple and prepared statements to the current Java driver doc.
You can use CQL collections with prepared statements. There's an example in the Java driver doc in the quick start section.
http://docs.datastax.com/en/developer/java-driver/2.0/java-driver/quick_start/qsSimpleClientBoundStatements_t.html
Step 4 binds a Java HashSet object to a CQL set column in the songs table.
Normally I'd ask what you've tried, but I know that this isn't in the DataStax documentation for the Java Driver. I'll go through what worked for me.
A couple of things to note:
The DataStax Cassandra Java class directed me to put my variables directly into the CQL text string (instead of binding the map). I'm guessing that binding collections wasn't working at the time of production (for the class videos).
Collections can't be queried using DevCenter, so you'll need to check their values via the command line with cqlsh if you want to see what they are outside your app.
To update an existing row (in the "users" table which has a Map<varchar,varchar> phone_numbers), try something like this:
String cqlQuery = "UPDATE users SET phone_numbers = phone_numbers + ";
cqlQuery += "{'" + phoneType + "':'" + phoneNumber+ "'} ";
cqlQuery += "WHERE username = ?";
PreparedStatement preparedStatement = getSession().prepare(cqlQuery);
BoundStatement boundStatement = preparedStatement.bind(user);
getSession().execute(boundStatement);
The better way to do this (assuming a Map<String,String> phoneNumbers), is to bind the collection to the prepared statement, like this:
String cqlQuery = "UPDATE users SET phone_numbers = ? ";
cqlQuery += "WHERE username = ?";
PreparedStatement preparedStatement = getSession().prepare(cqlQuery);
BoundStatement boundStatement = preparedStatement.bind(phoneNumbers,user);
getSession().execute(boundStatement);
Likewise, to read it back out:
String cqlQuery2 = "SELECT phone_numbers FROM users WHERE username = ?";
PreparedStatement preparedStatement2 = getSession().prepare(cqlQuery2);
BoundStatement boundStatement2 = preparedStatement2.bind(user);
ResultSet result2 = getSession().execute(boundStatement2);
Map<String,String> newMap = result2.one().getMap("phone_numbers", String.class, String.class);
They just covered this today in the (free) CAS101J class on DataStax Academy.
Here is how I did it; This has mapping for a tuple column in Cassandra as well as the map column in Cassandra. I was using Scala with the DataStax Cassandra Java Driver
Needed imports
import java.lang
import java.text.SimpleDateFormat
import com.datastax.driver.core._
import com.datastax.driver.core.querybuilder.QueryBuilder
import scala.collection.Map
import org.apache.spark.SparkContext
import org.apache.spark.SparkConf
import com.datastax.spark.connector.cql.CassandraConnector
import org.apache.spark.rdd.RDD
import scala.collection.JavaConversions._
Code snippet
val simpleDateFormat: SimpleDateFormat = new SimpleDateFormat("dd-MM-yyyy H:mm:ss")
val start_date: java.util.Date = simpleDateFormat.parse(val("StartTime").replaceAll(":(\\s+)", ":"))
val b_tuple= session.getCluster().getMetadata().newTupleType(DataType.cint(), DataType.cint(), DataType.text())
val time_tuple = session.getCluster().getMetadata().newTupleType(DataType.timestamp(), DataType.timestamp())
val time_tuple_value = time_tuple.newValue(start_date, end_date)
val b_tuple_value = bin_cell_tuple.newValue(b._1: lang.Integer, b._2: lang.Integer, val("xxx"))
val statement_2: Statement = QueryBuilder.insertInto("keyspace", "table_name")
.value("b_key", bin_cell_tuple_value)
.value("time_key", time_tuple_value)
.value("some_map", mapAsJavaMap(my_scala_map))
session.executeAsync(statement_2)

How to select All the Tables in Access DataBase 2007 using query

I am working in C#.net.I want to select all table names in the Access Database using Query. Can any one tell... How to select all the tables name from access database using query?.
Try like below it will help you...
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\myAccess2007file.accdb;Persist Security Info=False;";
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
DataTable tables = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[]{null,null,null,"TABLE"});
conn.Close();
Now the Datatable "tables" have all the access table Info..

Is there a fast way to get the number of rows in an Excel sheet using ADO.Net?

Is there a better way than
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=<xlxsName>;Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1;ReadOnly=True\"";
string sql = string.Format("SELECT COUNT (*) FROM [{0}] ", worksheetName);
using (OleDbConnection conn = new OleDbConnection(connectionString)) {
OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
totalCount = (int)cmd.ExecuteScalar();
conn.Close();
}
to get a row count from an excel sheet? If possible I'd prefer to use ADO.Net instead of Excel interop

Resources